四舍五入浮动值 [英] Rounding off floating values
本文介绍了四舍五入浮动值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何将浮点数四舍五入为int(非浮点值)
例如22.69至23
19.7至20
16.5至17
17.4至17
17.2至17
我试图将float或double转换为int,但没有得到想要的结果
坚强,我已经尝试过
双数= 19.7;
int number2 =(int)(数字)
number2 = 19;
这不是我想要达到的目标
在此先感谢
您的马丁
Hi there, how can i round off a floating number to an int (non floating value)
E.g 22.69 To 23
19.7 To 20
16.5 To 17
17.4 To 17
17.2 To 17
I have tried to cast float or double to int but i don''t get the desired results
Foristance,i have tried
double number = 19.7;
int number2=(int)(number)
number2=19;
This is not what i want to achieve
Thanks in advance
Yours Martin
推荐答案
尝试使用数学计算
您需要使用数学.舍入(值, MidpointRoundingMode ) [ ^ ].您需要使用AwayFromZero
选项.
在弄乱代码之前,请确保已阅读整个页面.
You need to use Math.Round(value, MidpointRoundingMode)[^]. You need to use theAwayFromZero
option.
Make sure you read the entire page before you go screwing up your code.
System.Math.Round(myNumber);
我认为是正确的.
System.Math.Round(myNumber);
That''s right I think.
这篇关于四舍五入浮动值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文