Math.Round有点烦人! [英] Math.Round is being a bit annoying!

查看:89
本文介绍了Math.Round有点烦人!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我读过如何围绕数字的文章。我基本上为控制台打印出了一点钱。这是总和:



So I''ve read the article in how to round numbers. And I''ve basically made a little sum for the console to print out. This is the sum:

int rndx = (int)Math.Round((decimal)(53 / 32), 0, MidpointRounding.AwayFromZero);
Console.WriteLine(rndx.ToString());





每次运行此代码时,它都会返回'' ''。虽然它应该是两个。



我甚至检查了我的计算器,其中53/32 = 1,65625。这将被四舍五入为2,因为它超过数字''x.5''。



任何答案?



Everytime I run this code, it returns as ''one''. While it should be two.

I''ve even checked my calculator which says that 53 / 32 = 1,65625. Which would be rounded up to 2 since it''s over number ''x.5''.

Any answers?

推荐答案

我希望它能返回一个! :笑:



为什么?简单:



53和32是整数。所以53/32也是一个整数,值为1.



整数值的 double 强制转换不会恢复丢失的分数数据...

尝试:

I expect it to return one! :laugh:

Why? Simples:

53 and 32 are integers. so 53 / 32 is also an integer, value 1.

The double cast of an integer value will not restore lost fractional data...
Try:
int rndx = (int)Math.Round(((decimal)53 / (decimal)32), 0, MidpointRounding.AwayFromZero);






Or

int rndx = (int)Math.Round((53M / 32M), 0, MidpointRounding.AwayFromZero);

其中指定十进制常数。



手指输入 - 脑部疾病。 double表示十进制:doh: - OriginalGriff [/ edit]

Which specifies decimal constants.

[edit]Fingers-typing-ahead-of-brain disease. "double" for "decimal" :doh: - OriginalGriff[/edit]


Math.Round [ ^ ]返回十进制值,而不是整数!点击链接!



参见其他方法:

Math.Floor() [ ^ ]

Math.Ceiling() [ ^ ]
Math.Round[^] returns decimal value, not integer! Follow the link!

See other methods:
Math.Floor()[^]
Math.Ceiling()[^]


这篇关于Math.Round有点烦人!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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