如何将值 C# 舍入到最接近的整数?
本文介绍了如何将值 C# 舍入到最接近的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 double 向上舍入为 int.
I want to round up double to int.
例如,
double a=0.4, b=0.5;
我想把它们都改成整数.
I want to change them both to integer.
这样
int aa=0, bb=1;
aa
来自 a
而 bb
来自 b
.
aa
is from a
and bb
is from b
.
有什么公式可以做到这一点吗?
Any formula to do that?
推荐答案
使用 Math.Ceiling
向上舍入
Use Math.Ceiling
to round up
Math.Ceiling(0.5); // 1
使用 Math.Round
只是圆
Use Math.Round
to just round
Math.Round(0.5, MidpointRounding.AwayFromZero); // 1
和 Math.Floor
向下舍入
And Math.Floor
to round down
Math.Floor(0.5); // 0
这篇关于如何将值 C# 舍入到最接近的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2
如何将值 C# 舍入到最接近的整数? 为WP2原创文章,链接:https://www.wp2.cn/other/%e5%a6%82%e4%bd%95%e5%b0%86%e5%80%bc-c-%e8%88%8d%e5%85%a5%e5%88%b0%e6%9c%80%e6%8e%a5%e8%bf%91%e7%9a%84%e6%95%b4%e6%95%b0/