C# decimal取整
时间: 2023-09-23 18:08:11 浏览: 251
C#编程实现取整和取余的方法
在C#中,decimal类型可以使用Floor、Truncate和Round方法来进行取整操作。其中,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.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/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 ]
阅读全文