Strange decimals are added to result [duplicate] - c#

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.

Related

C# unexpected result when casting to float [duplicate]

This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
Closed 1 year ago.
Try this:
(float)100008009
And you will probably get
100008008
The issue is that we get no warning. And this can't be overflow since floats can take higher values. So I can't explain this result.
What is the Max value for 'float'?
The issue is that we get no warning.
Floating-point is intended to approximate real-number arithmetic. So rounding during conversion is part of the design, meaning it is normal, so it does not get a warning. The closest value to 100008009 representable in float is 100008008, so that is the result.

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.

Double.TryParse shows wrong result [duplicate]

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

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