How to convert a double to a long without truncating it?
For example,
I want -26.3999745 to become -263999745
So far the methods I have tried such as Convert.ToInt64() truncate the number.
EDIT:
The number of decimal places varies, some of the numbers may have 5 decimal places
If you are really sure this is what you really want ..
long.Parse((-26.3999745).ToString().Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToString(), string.Empty)))
Following 2 steps
Get the numbers after the decimal n
Multiply by 10^n
Code:
double number = -26.3999745;
int n = BitConverter.GetBytes(decimal.GetBits((decimal)number)[3])[2];
long result = (long)(number * Math.Pow(10, n));
If you like to follow Single Responsibility (you should), then you can go with a second approach that is also not culture specific:
static int GetCountAfterDecimal(double number)
{
int count = 0;
string seperator = System.Globalization.CultureInfo.CurrentCulture
.NumberFormat.NumberDecimalSeparator;
string numberAsString = number.ToString();
int indexOfSeperator = numberAsString.IndexOf(seperator);
if (indexOfSeperator >= 0)
count = numberAsString.Length - indexOfSeperator - 1;
return count;
}
static long RemoveDecimalPoint(double number, int numbersCountAfterDecimal)
{
return (long)(number * Math.Pow(10, numbersCountAfterDecimal));
}
static void Main(string[] args)
{
double number = -26.3999745;
long result = RemoveDecimalPoint(number, GetCountAfterDecimal(number));
Console.WriteLine(result);
}
This might do the trick for you
double YourNumber = -26.3999745;
int DecimalPlacesCount = BitConverter.GetBytes(decimal.GetBits(Convert.ToDecimal(YourNumber))[3])[2];
long number = Convert.ToInt64("1".PadRight(DecimalPlacesCount + 1, '0'));
long kajs = (long) (YourNumber * number);
Related
i am adding two decimal values like
decimal one=0; decimal two=0; decimal sum=0;
sum = one+ two;
Int final =0;
example
sum = 1.2 + 2.2;
sum = 3.4;
Now i want to save this 3.4 in Integer Final by neglecting that decimal part (.4). How can I do this?
class Program
{
static void Main(string[] args)
{
decimal one = 1.4M; decimal two = 3.4M; decimal sum = 0;
sum = one + two;
Int32 final = (Int32)(sum);
Int32 roundfinal = (Int32)(Math.Round(sum));
Console.WriteLine("final is "+ final);
Console.WriteLine("roundfinal is " + roundfinal);
Console.ReadLine();
}
}
check the answer without round rumber and with round number 4.8 is 4 without round and 4.8 round is 5
What you're looking for!:
final = Convert.ToInt32(sum);
Or:
final = (int)sum;
Decimal value = Decimal.Add(a1, a2);
FinalOutout int = Convert.ToInt32(value);
It will work.
I have an input type integer that represents a number that needs to be converted to double between 1-100, and the rest is decimal precision.
Example: 1562 -> 15.62 ; 198912 -> 19.8912
Right now, I tried a conversion to string, count the number of characters, take 2 to check how many decimals I have and depending of the result "create" a composite string to get a valid double...
Any idea of there is a better way of resolving convert-precision on runtime.
What about this:
int value = 1562;
decimal d = value;
while (d > 100) {
d /= 10;
}
You can use LINQ Skip and Take like:
string str = "198912";
string newStr = string.Format("{0}.{1}", new string(str.Take(2).ToArray()), new string(str.Skip(2).ToArray()));
double d = double.Parse(newStr, CultureInfo.InvariantCulture);
You can add the checks for length on original string, and also use double.TryParse to see if you get valid values.
If you have an int to begin with then you can use decimal, which would provide you more accurate conversion. Like:
int number = 1562123123;
decimal decimalNumber = number;
while (decimalNumber > 100)
{
decimalNumber /= 10;
}
Here is a mathematical solution. The line lg = Math.Max(lg, 0); changes "2" to return "2.0" instead of "20.0" but I guess that depends on your needs for single digit numbers.
static double ToDoubleBetween1And100(int num)
{
var lg = Math.Floor(Math.Log10(num)) - 1;
lg = Math.Max(lg, 0);
return ((double)num) / Math.Pow(10, lg);
}
I'm trying to create a decimal representation of a big integer when it's divided by something. The follwoing is the code which does it, basically I want the precision to be of 2 places.
public string GetDecimal(BigInteger bigInteger,int divisor)
{
var remainder = BigInteger.Remainder(bigInteger, divisor);
var dividend = BigInteger.Divide(bigInteger, divisor);
var d = ((double)remainder / divisor);
var decimalPart = Math.Round(d, 2);
var retValue = dividend + decimalPart.ToString(".00");
return retValue;
}
}
Is there a better way of doing this please?
Thanks,
-Mike
You should probably not convert the types and do the long division on your own. This should work with any BigInteger value.
I'm sure there's room for improvement here...
public string GetDecimal(BigInteger bigInteger, int divisor)
{
BigInteger remainder;
var quotient = BigInteger.DivRem(bigInteger, divisor, out remainder);
const int decimalPlaces = 2;
var decimalPart = BigInteger.Zero;
for(int i = 0; i < decimalPlaces; i++)
{
var div = (remainder*10)/divisor;
decimalPart *= 10;
decimalPart += div;
remainder = remainder*10 - div*divisor;
}
var retValue = quotient.ToString() + "." + decimalPart.ToString(new string('0', decimalPlaces));
return retValue;
}
The built in .net decimal type is 128bit, and works with similar rounding constructs of other types. Is this number not large enough?
Instead of doing maths on it especially using less accurate types like double.
simply build a string with all but the last two digits, append a decimal point and then put the last one in.
e.g. Something like
int precision = 2;
negative = 0;
if (bigInteger < 0)
{
negative = 1;
}
String strValue = bigInteger.ToString().PadRight(precision + negative + 1,'0');
return strValue.Insert(strValue.Length - precision, ".");
var s=((bigInteger*200+divisor)/(2*(BigInteger)divisor)).ToString();
return s.Insert(".",s.Length-2);
This code only works for positive values, and uses AwayFromZero midpoint rounding. I also didn't care about localization issues.
In my C# program I have a double obtained from some computation and its value is something like 0,13999 or 0,0079996 but this value has to be presented to a human so it's better displayed as 0,14 or 0,008 respectively.
So I need to round the value, but have no idea to which precision - I just need to "throw away those noise digits".
How could I do that in my code?
To clarify - I need to round the double values to a precision that is unknown at compile time - this needs to be determined at runtime. What would be a good heuristic to achieve this?
You seem to want to output a value which is not very different to the input value, so try increasing numbers of digits until a given error is achieved:
static double Round(double input, double errorDesired)
{
if (input == 0.0)
return 0.0;
for (int decimals = 0; decimals < 17; ++decimals)
{
var output = Math.Round(input, decimals);
var errorAchieved = Math.Abs((output - input) / input);
if (errorAchieved <= errorDesired)
return output;
}
return input;
}
}
static void Main(string[] args)
{
foreach (var input in new[] { 0.13999, 0.0079996, 0.12345 })
{
Console.WriteLine("{0} -> {1} (.1%)", input, Round(input, 0.001));
Console.WriteLine("{0} -> {1} (1%)", input, Round(input, 0.01));
Console.WriteLine("{0} -> {1} (10%)", input, Round(input, 0.1));
}
}
private double PrettyRound(double inp)
{
string d = inp.ToString();
d = d.Remove(0,d.IndexOf(',') + 1);
int decRound = 1;
bool onStartZeroes = true;
for (int c = 1; c < d.Length; c++ )
{
if (!onStartZeroes && d[c] == d[c - 1])
break;
else
decRound++;
if (d[c] != '0')
onStartZeroes = false;
}
inp = Math.Round(inp, decRound);
return inp;
}
Test:
double d1 = 0.13999; //no zeroes
double d2 = 0.0079996; //zeroes
double d3 = 0.00700956; //zeroes within decimal
Response.Write(d1 + "<br/>" + d2 + "<br/>" + d3 + "<br/><br/>");
d1 = PrettyRound(d1);
d2 = PrettyRound(d2);
d3 = PrettyRound(d3);
Response.Write(d1 + "<br/>" + d2 + "<br/>" + d3 +"<br/><br/>");
Prints:
0,13999
0,0079996
0,00700956
0,14
0,008
0,007
Rounds your numbers as you wrote in your example..
I can think of a solution though it isn't very efficient...
My assumption is that you can tell when a number is in the "best" human readable format when extra digits make no difference to how it is rounded.
eg in the example of 0,13999 rounding it to various numbers of decimal places gives:
0
0.1
0.14
0.14
0.14
0.13999
I'd suggest that you could loop through and detect that stable patch and cut off there.
This method seems to do this:
public double CustomRound(double d)
{
double currentRound = 0;
int stability = 0;
int roundLevel = 0;
while (stability < 3)
{
roundLevel++;
double current = Math.Round(d, roundLevel);
if (current == currentRound)
{
stability++;
}
else
{
stability = 1;
currentRound=current;
}
}
return Math.Round(d, roundLevel);
}
This code might be cleanable but it does the job and is a sufficient proof of concept. :)
I should emphasise that that initial assumption (that no change when rounding) is the criteria we are looking at which means that something like 0.3333333333 will not get rounded at all. With the examples given I'm unable to say if this is correct or not but I assume if this is a double issues that the problem is with the very slight variations from the "right" value and the value as a double.
Heres what I tried:
public decimal myRounding(decimal number)
{
double log10 = Math.Log10((double) number);
int precision = (int)(log10 >= 0 ? 0 : Math.Abs(log10)) + (number < 0.01m ? 1 : 2);
return Math.Round(number, precision);
}
test:
Console.WriteLine(myRounding(0.0000019999m)); //0.000002
Console.WriteLine(myRounding(0.0003019999m)); //0.0003
Console.WriteLine(myRounding(2.56777777m)); //2.57
Console.WriteLine(myRounding(0.13999m)); //0.14
Console.WriteLine(myRounding(0.0079996m)); //0.008
You can do it without converting to string. This is what I created fast:
private static double RoundDecimal(double number)
{
double temp2 = number;
int temp, counter = 0;
do
{
temp2 = 10 * temp2;
temp = (int)temp2;
counter++;
} while (temp < 1);
return Math.Round(number, counter < 2 ? 2 : counter);
}
or
private static double RoundDecimal(double number)
{
int counter = 0;
if (number > 0) {
counter = Math.Abs((int) Math.Log10(number)) + 1;
return Math.Round(arv, counter < 2 ? 2 : counter);
}
After giving it another thought I did the following and looks like it does what I want so far.
I iterate over the number of digits and compare Round( value, number ) and Round( value, number + 1 ). Once they are equal (not == of course - I compare the difference against a small number) then number is the number of digits I'm looking for.
Double.ToString() can take a string format as an argument. This will display as many characters as you require, rounding to the decimal place. E.G:
double Value = 1054.32179;
MessageBox.Show(Value.ToString("0.000"));
Will display "1054.322".
Source
Generic formats (i.e, pre-generated)
How to generate custom formats
You can use no of digits with Math.Round Function
Double doubleValue = 4.052102;
Math.Round(doubleValue, 2);
This will return 4.05 as your required answer.
This is tested code, can u explain me how i am wrong. So i need to change.
I need to split an double value, into two int value, one before the decimal point and one after. The int after the decimal point should have two digits.
Example:
10.50 = 10 and 50
10.45 = 10 and 45
10.5 = 10 and 50
This is how you could do it:
string s = inputValue.ToString("0.00", CultureInfo.InvariantCulture);
string[] parts = s.Split('.');
int i1 = int.Parse(parts[0]);
int i2 = int.Parse(parts[1]);
Manipulating strings can be slow. Try using the following:
double number;
long intPart = (long) number;
double fractionalPart = number - intPart;
What programming language you want to use to do this? Most of the language should have a Modulo operator. C++ example:
double num = 10.5;
int remainder = num % 1
"10.50".Split('.').Select(int.Parse);
/// <summary>
/// Get the integral and floating point portions of a Double
/// as separate integer values, where the floating point value is
/// raised to the specified power of ten, given by 'places'.
/// </summary>
public static void Split(Double value, Int32 places, out Int32 left, out Int32 right)
{
left = (Int32)Math.Truncate(value);
right = (Int32)((value - left) * Math.Pow(10, places));
}
public static void Split(Double value, out Int32 left, out Int32 right)
{
Split(value, 1, out left, out right);
}
Usage:
Int32 left, right;
Split(10.50, out left, out right);
// left == 10
// right == 5
Split(10.50, 2, out left, out right);
// left == 10
// right == 50
Split(10.50, 5, out left, out right);
// left == 10
// right == 50000
how about?
var n = 1004.522
var a = Math.Floor(n);
var b = n - a;
Another variation that doesn't involve string manipulation:
static void Main(string[] args)
{
decimal number = 10123.51m;
int whole = (int)number;
decimal precision = (number - whole) * 100;
Console.WriteLine(number);
Console.WriteLine(whole);
Console.WriteLine("{0} and {1}",whole,(int) precision);
Console.Read();
}
Make sure they're decimals or you get the usual strange float/double behaviour.
you can split with string and then convert into int ...
string s = input.ToString();
string[] parts = s.Split('.');
This function will take time in decimal and converts back into base 60 .
public string Time_In_Absolute(double time)
{
time = Math.Round(time, 2);
string[] timeparts = time.ToString().Split('.');
timeparts[1] = "." + timeparts[1];
double Minutes = double.Parse(timeparts[1]);
Minutes = Math.Round(Minutes, 2);
Minutes = Minutes * (double)60;
return string.Format("{0:00}:{1:00}",timeparts[0],Minutes);
//return Hours.ToString() + ":" + Math.Round(Minutes,0).ToString();
}
Try:
string s = "10.5";
string[] s1 = s.Split(new char[] { "." });
string first = s1[0];
string second = s1[1];
You can do it without going through strings. Example:
foreach (double x in new double[]{10.45, 10.50, 10.999, -10.323, -10.326, 10}){
int i = (int)Math.Truncate(x);
int f = (int)Math.Round(100*Math.Abs(x-i));
if (f==100){ f=0; i+=(x<0)?-1:1; }
Console.WriteLine("("+i+", "+f+")");
}
Output:
(10, 45)
(10, 50)
(11, 0)
(-10, 32)
(-10, 33)
(10, 0)
Won't work for a number like -0.123, though. Then again, I'm not sure how it would fit your representation.
I actually just had to answer this in the real world and while #David Samuel's answer did part of it here is the resulting code I used. As said before Strings are way too much overhead. I had to do this calculation across pixel values in a video and was still able to maintain 30fps on a moderate computer.
double number = 4140 / 640; //result is 6.46875 for example
int intPart = (int)number; //just convert to int, loose the dec.
int fractionalPart = (int)((position - intPart) * 1000); //rounding was not needed.
//this procedure will create two variables used to extract [iii*].[iii]* from iii*.iii*
This was used to solve x,y from pixel count in 640 X 480 video feed.
Console.Write("Enter the amount of money: ");
double value = double.Parse(Console.ReadLine());
int wholeDigits = (int) value;
double fractionalDigits = (value - wholeDigits) * 100;
fractionalDigits = (int) fractionalDigits;
Console.WriteLine(
"The number of the shekels is {0}, and the number of the agurot is {1}",
wholeDigits, fractionalDigits);
Using Linq. Just clarification of #Denis answer.
var parts = "10.50".Split('.').Select(int.Parse);
int i1 = parts.ElementAt(0);
int i2 = parts.ElementAt(1);