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

北慕城南 提交于 2020-01-04 04:10:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!