Double.TryParse shows wrong result [duplicate] - c#

This question already has answers here:
there is a strange behaviour double parse string input?
(3 answers)
Closed 7 years ago.
I have a string which comes from database. For example it is 5.1 . I want to convert this string to double with double.tryparse() method. I expect the result will be 5.1, but it is not. The result seems like 5.0999999999999996. What can I do to achieve this so that it will be 5.1?

Instead of double, do :
decimal.TryParse(s, out myDecimal);
or
decimal d = Convert.ToDecimal(s);
Because decimal is floating decimal point instead of floating binary.
Here's a great explanation : Difference between double and decimal

Related

Strange decimals are added to result [duplicate]

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.

Why 60.5 - 60.1 is 0.399999999999999 ? In C# double variable [duplicate]

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.

C# double/decimal always have two decimals [duplicate]

This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 7 years ago.
I need 45.7 to be 45.70
Math.Round(d, 2) have no effect.
Have tried with decimal and double type.
I would appreciate any kind of help.
You need to apply a string format:
string.Format("{0:N2}", 45.7)
Check here for docs on the string formatters

How to determine the # of decimals in a double? [duplicate]

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
;

string.format() with at least one decimal place [duplicate]

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

Categories

Resources