C# 带小数点儿的字符串(如:"100.5"),如何只取整数部分

海贝   2016年8月11日 浏览量:1118

1. Math.Floor & Math.Ceiling(Floor——出头舍;Ceiling——出头算)

 

Math.Floor:

返回小于或等于指定小数的最大整数值。

 

Math.Floor(3.21);//3

Math.Floor(5.952);//5

Math.Floor(0.64);//0

Math.Floor(-0.64);//-1

Math.Floor(-5.2);//-6

Math.Floor(-5.8);//-6

 

Math.Ceiling:

返回大于或等于指定小数的最小整数值。



 

Math.Ceiling(3.21);//4

Math.Ceiling(5.952);//6

Math.Ceiling(0.64);//1

Math.Ceiling(-0.64);//0

Math.Ceiling(-5.2);//-5

Math.Ceiling(-5.8);//-5

 

注意:

返回类型为Double或者Decimal。


 

 

2. Math.Truncate 取整/截断

 

作用:

计算指定小数的整数部分。

 

示例:

Math.Truncate(3.21);//3

Math.Truncate(5.952);//5

Math.Truncate(0.64);//0

Math.Truncate(-0.64);//0

Math.Truncate(-5.2);//-5

Math.Truncate(-5.8);//-5

 

注意:

返回类型为Double或者Decimal。


 

 

3. Math.Round 舍入

 

作用:

Math.Round采取的舍入方式和Convert.ToInt32(Double)一样,都是使用bankers' rounding 规则(四舍六入五成双)

 

示例:

Math.Round(3.21);//3

Math.Round(5.5);//6

Math.Round(4.5);//4

Math.Round(-0.64);//-1

Math.Round(-5.5);//-6

Math.Round(-4.5);//-4

 

小数取舍:

Math.Round(Double, Int32),其中Int32指定返回值的小数位数。


 

示例:

Math.Round(3.44, 1); //Returns 3.4.

Math.Round(3.45, 1); //Returns 3.4.

Math.Round(3.46, 1); //Returns 3.5.



Math.Round(4.34, 1); // Returns 4.3

Math.Round(4.35, 1); // Returns 4.4

Math.Round(4.36, 1); // Returns 4.4

加载中...

正在加载更多内容...

更新日期:2016年8月11日
关键字:海贝
免责声明:文章或资料来源于网络,如有关于本文内容、版权或其它问题请于文章发表后的30日内与本网管理员联系删除或修改。

01/1|<<1>>|