C#取整 之 Math取整

C# 之 Math取整
主要用到 System 命名空间下的一个数据类 Math ,调用他的方法。

C#取整函数使用详解:
1、Math.Round是"就近舍入",当要舍入的是5时与"四舍五入"不同(取偶数),如:
Math.Round(0.5,0)=0    
Math.Round(1.5,0)=2    
Math.Round(2.5,0)=2    
Math.Round(3.5,0)=4 

2、Math.Truncate 计算双精度浮点数的整数部分,即直接取整数,如:
Math.Truncate(-123.55)=-123, 
Math.Truncate(123.55)=123   

3、Math.Ceiling 取天板值,即向上取整,与"四舍五入"无关。
Math.Ceiling(1) = 1
Math.Ceiling(1.1) = 2
Math.Ceiling(1.5) = 2
Math.Ceiling(3.1) = 4 

4、Math.Floor 取地板值,即向下取整,与"四舍五入"无关。
Math.Floor(1) = 1
Math.Floor(1.1) = 1
Math.Floor(1.5) = 1
Math.Floor(3.9) = 3
   
取天板值与地板值,与"四舍五入"无关。其实Floor的结果与(int)相同,因此也可以这样写Math.Floor((double)2/3+0.5)
int a = 5; int b = 2;   lbl.Text = Convert.ToString(Math.Ceiling((double)a / (double)b)); 

 

转载于:https://www.cnblogs.com/ruralcraftsman/p/11365417.html

C#中,可以使用Math.Floor()方法来对decimal类型进行取整操作。该方法会向下取整,即将小数部分舍去,返回一个整数值。例如,Math.Floor(3.2m)的结果是3,Math.Floor(-4.7m)的结果是-5。 此外,还可以使用Math.Truncate()方法来对decimal类型进行取整。该方法会将小数部分截断,返回一个整数值。例如,Math.Truncate(3.2m)的结果是3,Math.Truncate(-4.7m)的结果是-4。 总结起来,C#中的decimal类型可以使用Math.Floor()和Math.Truncate()方法来进行取整操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#decimal结构取整方法总结](https://blog.csdn.net/sammy520/article/details/108225237)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C#编程实现取整和取余的方法](https://download.csdn.net/download/weixin_38752897/14869537)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C#常见金额优选类型及其三种常用的取整方式](https://blog.csdn.net/dfj66154/article/details/101219133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值