I am converting a decimal of two digits after the decimal but it is not exact when it is converted to the double.
Example:
value inputted into textbox: 0.56
double value = Convert.ToDouble(txtInput.Text)
when i go into debug mode value actually is equaling 0.559999999872
I am so confused and it is throwing off some of my calculations slightly. Is there a way I could cut the decimal off after two digits after the decimal?
Thanks

1 answers
Haha do you own a Pentium?!? LOL.
Maybe you can try this:
<b>
double value = Convert.ToDouble(txtInput.Text);
value = (int)(value * 100) / 100;
oh I also think there might be a math function dedicated to that, like
Math.Fix(value, 2); or something.
answered 2 years ago by:
60
Thanks for the reply!
The 100/100 thing didn't work because it needs to be a double not a casted int.
I found out what the function was "Math.Truncate" but I don't know how to implement that so it keeps only 2 digits after the decimal.
answered 2 years ago by:
30
I actually figured out the solution to my problem by using the Math.Round(value,2) function.
answered 2 years ago by:
30
If you want to avoid the rounding problems which you sometimes get with the floating point types (double and float), then use the decimal type instead.
It needs more memory and is slower for intensive calculations but, for most financial applications, it's still the best choice.
answered 2 years ago by:
17279
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!