Math.Round四舍六入五成双和double.tostring()的用法

Math.Round (Decimal, Int32) 将小数值舍入到指定精度。
由 .NET Compact Framework 支持。
Math.Round (Double, Int32) 
将双精度浮点值舍入到指定精度。
由 .NET Compact Framework 支持。

这个函数的正确用法:
3.4 = Math.Round( 3.45, 1)
3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)


在C#中大家都会遇到这种情况 double类型的数据,需要格式化(保留N未有效数字)或者是保留N为小数等情况,我们往往采取double.tostring("参数");的方法。下面就列出几个常用的方法。

double temp=3.1415926;

(F)Fixed point:string str1=temp.toString("f1");//保留一位小数 四舍五入 结果:3.1

(F)Fixed point:string str2=temp.toString("f2");//保留两位小数,四舍五入 下面一次类推 结果:3.14

(N)Number:string str2=temp.toString("N");//保留 结果:3.14

(G)General (default):string str2=temp.toString("G");//保留 结果:3.1415926

(P)Percent:string str2=temp.toString("P");//保留 结果:314.16%

(E)Scientific:string str2=temp.toString("E");//保留 结果E:3.141593E+000

(C)Currency:string str2=temp.toString("C");//保留 结果:¥3.14


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值