Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precision of C# decimal type division is different on Mono?

decimal d1 = 4.0m;
decimal d2 = 40.0m;
decimal d = d1 / d2;
string repr = d.ToString();

On Windows, I get "0.1" for repr. On Mono (Xamarin Android), I get "0.1000000000000000000000000000" or so. What gives? Also, I've seen recommendations to divide by 1.0000000000000000000000000000m to get rid of unwanted trailing zeroes. That doesn't work either on Xamarin Android.

Known bug? Or technically not a bug - is this allowed in conformance with the standard?

To get rid of the zeroes, I'm converting the value to a string, removing trailing zeroes from the string, and converting it back to a decimal. It's pretty weak. Also, my solution doesn't produce the exact results I'd want. I'd prefer to leave trailing zeroes that are appropriately significant, rather than removing all trailing zeroes.

like image 484
morningstar Avatar asked Dec 05 '25 19:12

morningstar


1 Answers

Maybe it's worth a note, that this bug has been fixed, as stated by Miguel de Icaza: https://bugzilla.novell.com/show_bug.cgi?id=655780#c2

like image 170
Piotr Zierhoffer Avatar answered Dec 08 '25 09:12

Piotr Zierhoffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!