Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

C#四舍五入的几种方法

在我们平时的计算当中,经常会对小数点后的数字进行四舍五入的操作,那么在C#中,是如何实现四舍五入呢? 往下看

第一种:使用 Round() 方法输出

double dValue = 1880.875;
double d = Math.Round(dValue, 2); //输出:1880.88
decimal de = decimal.Round(decimal.Parse(dValue), 2); //输出:1880.88

第二种:使用 ToString() 方法输出

double dValue = 612.576;
string str = dValue.ToString("f2"); //输出:612.58
string str1 = dValue.ToString("#0.00"); //输出:612.58,小数点后有几个0就保留几位

第三种:使用 Format() 方法输出

double dValue = 201.38769;
string str1 = String.Format("{0:N2}", dValue); //输出:201.39
string str2 = String.Format("{0:N3}", dValue); //输出:201.388
string str3 = String.Format("{0:N4}", dValue); //输出:201.3877

但是你需要反复计算取精度的话,我们建议使用Round() 方法,这样可以减少一些偏差。

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
未整理

直播带货怎么做?商家如何玩转直播带货,针对商家 内容干货 目的赚钱

2021-5-5 0:00:00

未整理

云优YUNUCMS企业网站管理系统

2021-5-7 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索