[英]Custom Math.Round
我想四舍五入到小数点后,如果大于0则向上舍入到下一个数字。
例:
2.34121 -> 2.3413
50.58020 -> 50.5802
4.49238 -> 4.4924
0.00001 -> 0.0001
而且Math.Round只能从中点向上或向下舍入。
尝试这个:
double CustomRound(double x) => Math.Ceiling(x * 10000) / 10000;
public static class Extension
{
public static double Convert(this double d)
{
var d1 = (int)(d * 10000);
var d2 = (int)((d * 1000) * 10);
if ((d1 - d2) != 0)
{
return Math.Round(d, 4) + 0.0001;
}
return Math.Round(d, 4);
}
}
然后调用2.34121.Convert();
舍入前只需添加0.00004。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.