如何通过除以小数来获得准确的值 [英] How to get an accurate value by dividing decimals

查看:103
本文介绍了如何通过除以小数来获得准确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

I have this code

decimal total = 2m;
decimal discount = 0.2m;
decimal disPercent = discount / total;





disPercent值应为0.0909090909090909

但我得到0.1

我该怎么办才能得到0.0909090909090909?





请注意值是例如,我总是从数据库中得到它们作为十进制价值



我的尝试:





disPercent value should be 0.0909090909090909
but I get 0.1
what should I do to get 0.0909090909090909?


note that the values are for example, I always get them from database as decimal values

What I have tried:

decimal total = 2m;
decimal discount = 0.2m;
decimal disPercent = decimal.Divide(discount, total);



无法正常工作


not working

推荐答案

为什么你认为0.2 / 2.0是0.0909090909 .. 。

我小时候教过的数学说不同:

Why do you think that 0.2 / 2.0 is 0.0909090909...
The maths I was taught as a child says different:
0.2 / 2.0 == 2.0 / 20.0 == 1.0 / 10.0 == 0.1


0.2 / 2实际上等于0.1。我认为你的问题可能需要在总数中包含0.2,所以支付2.0,折扣0.2,总共2.2。然后,如果你将0.2除以2.2,你将得到0.0909重复的预期结果(实际上是1/11)。
0.2 / 2 does, in fact, equal 0.1. I am thinking that your question may have needed to include the 0.2 in the total, so 2.0 is paid, 0.2 is discount, and 2.2 is total. Then if you divide 0.2 by 2.2, you will get your expected result of 0.0909 recurring (effectively 1/11).


除以小数已经返回一个准确的值(比使用任何数学运算更准确浮点值)。
Dividing decimals already returns an accurate value (more accurate than doing ANY math with floating point values).


这篇关于如何通过除以小数来获得准确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆