1.23 确定一个Decimal或Double的整数部分

知识点:

1.System.Math.PI

2.System.Math.Truncate() //取整

问题:

需要找出一个decimal 或 double数的整数部分。

解决方案

只要将一个decimal 或 double 数截断为最接近于0的数,就可以得到其整数部分。为此,可以使用重载的静态System.Math.Truncate方法,这个方法取一个decimal或一个double作为参数,并返回同样的类型。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _07确定一个Decimal或Double的整数部分
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             decimal pi = (decimal)System.Math.PI;
14             decimal decRet = System.Math.Truncate(pi);
15             Console.WriteLine(decRet);
16             Console.ReadKey();
17 
18             double trouble = 5.555;
19             double decRet2 = System.Math.Truncate(trouble);
20             Console.WriteLine(decRet2);
21             Console.ReadKey();
22         }
23     }
24 }
View Code

验证结果

1.decRet = 3

2.decRet2 = 5

原文地址:https://www.cnblogs.com/weijieAndy/p/3990299.html