This question already has answers here:
Find number of decimal places in decimal value regardless of culture
(20 answers)
Closed 8 years ago.
If I have a number, how can I determine the number of decimals?
e.g. for 0.0001 I want to get the result 4
The duplicate suggested above is less suitable than this one because
they are taking about culture-independent code but this question is
just about decimal oriented code (i.e. after the decimal). So no need
to introduce any more overhead:
Finding the number of places after the decimal point of a Double
but they both are good threads.
You can't really. A double is a floating point precision data type, so it's never precise.
You could hack something around, using ToString:
double d = 0.994562d;
int numberOfDecimals = d.ToString(CultureInfo.InvariantCulture).Length
- d.ToString(CultureInfo.InvariantCulture).IndexOf('.')
- 1
;
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
C# adds a decimal at the end in the result, my code:
public static double CalcCompoundedInterest()
{
return (1.1 * 1.1);
}
Result: 1.2100000000000002
Does someone have a clue why this happens?
This is not a C# problem, this is the way computers work when they handle decimal values.
You see, 1.1 is stored as a float, which is encoded in binary using the IEEE 754 standard. Most decimals numbers are not possible to store without adding a very small error to them.
This question already has answers here:
Possible Loss of Fraction
(5 answers)
Closed 4 years ago.
I'm trying to print out this integer variable which in the case of my test numbers should be 5,2333 but only 5 gets printed.
int finalTimeInt = ((N * 60 + n) / 2) / 60;
Console.Writeline(finalTimeInt);
As stated already I'm expecting an output of 5,2333 but am only getting a 5.
Integers can't hold decimals. If you want a variable to store decimals, you should be using a floating-point type, like float, or double if you want more accuracy.
This question already has answers here:
Floating point inaccuracy examples
(7 answers)
Closed 6 years ago.
double a=60.5;
double a=60.1;
Console.WriteLine(a-b);
Return value is 0.399999999999999 not 0.4
It is because you use double: double is floating point, which is not precise; decimal, on the other hand, is precise. If you change both variable to decimal, it will be the exact number.
That is why in certain domains, like financial industry, decimal is desired for accuracy and precision.
This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Closed 6 years ago.
I have a small question regarding Math.Round function.
I need the string "12.123456" to be rounded at 4 decimals. I used:
Math.Round(Convert.ToDouble(pData), 4).ToString()
where pData is defined as string, but the values are decimal with 7 decimals.
My problem is that I expected to get every time the exact 4 decimals, but for some values it gives me only 2 (eg. 12.12 instead of 12.1200).
How can I change in order to always get the needed 4 decimals?
Regards,
You should use format strings instead:
pDate.ToString("0.0000")
or
pDate.ToString("n4")
This question already has answers here:
Display Float as String with at Least 1 Decimal Place
(4 answers)
Closed 7 years ago.
From the following two - double precision - numbers:
123456
0.0003232
I need to get (at least one decimal place) ...
123456.0 (one decimal place added)
0.0003232 (same as above)
... and never scientific notation, like E+000. The more close result from standard string.Format() is string.Format("{0:F1}", myDoubleVal) but in the second case the decimals are lost.
What else can I try?
Thanks.
Try the below
string.Format("{0:0.0###########}",myval);
Thanks