c#中取整和取余

"%"为取余号,不用多说。   
"/"号现在整形运算是取整,浮点运算时为除法运算,如54/10结果为5,54.0/10.0结果为5.4而且取整时不进行四舍五入只取整数部分,如54/10和56/10是5.   
Math.Celling()取整数的较大数,相当于不管余数是什么都会进一位。如Math.Celling(54.0/10.0)结果为6.   
Math.Floor()取整数的较小数,相当于"/"号,即不管余数部分是什么都不进行进位。如Math.Floor(56.0/10.0)的结果是5.
代码如下: using System;
using System.Collections.Generic;
 using System.Text;
namespace ConsoleApplication5
 { class Program { static void Main(string[] args)
 { Console.WriteLine("(54/10):{0}", 54 / 10);
Console.WriteLine("(56/10):{0}", 56/ 10);
Console.WriteLine("(54.0%10.0):{0}", 54.0 % 10.0);
Console.WriteLine("(56.0%10.0):{0}", 56.0 % 10.0);
Console.WriteLine("Math.Celling(54.0/10.0):{0}", Math.Ceiling(54.0 / 10.0));
Console.WriteLine("Math.Celling(56.0/10.0):{0}", Math.Ceiling(56.0 / 10.0));
 Console.WriteLine("Math.Floor(54.0/10.0):{0}", Math.Floor(54.0 / 10.0));
 Console.WriteLine("Math.Floor(56.0/10.0):{0}", Math.Floor(56.0 / 10.0)); } } }
C#中,关于除法"/"运算得一点问题。
  现在C#与法中,"/"除后所得的值的类型,跟他的除数和被除数的类型有关。如: int a=4; int b=5; float c=a/b ;   则结果为0(因为会先进行int的除法操作,得出结果0,再将结果转为float 0;);   总之,得出的数都是整形的,最终发觉原来除后所得的值的类型,跟他的除数和被除数的类型有关。所以,应写成: float a=3; float b=5; float c=a/b; 这样,才能得出正确的结论!
C#中,decimal类型可以使用Floor、TruncateRound方法来进行取整操作。其中,Floor方法会向负无穷方向舍入为最接近的整数,而Truncate方法则会向零方向舍入为整数。另外,如果想实现四舍五入的效果,可以使用Round方法,并将小数保留到指定位数后再进行舍入操作。具体可以参考以下代码实例: using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { decimal a = 8.335m, b = 8.345m; Console.WriteLine(decimal.Floor(a)); // 8 Console.WriteLine(decimal.Truncate(b)); // 8 Console.WriteLine(decimal.Round(a, 2)); // 8.34 Console.WriteLine(decimal.Round(b, 2)); // 8.35 } } } 通过以上代码,可以看到在C#中使用decimal类型进行取整操作是相当简单的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#decimal结构取整方法总结](https://blog.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/feiba527/article/details/50330327)[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、付费专栏及课程。

余额充值