问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/26414708/precision-of-c-sharp-decimal-type-division-is-different-on-mono