C#中decimal ,double,float的区别

浮点型

 

Name

 

CTS Type

 

Description

 

Significant Figures

 

Range (approximate)

 

float

 

System.Single

 

32-bit single-precision floating point

 

7

 

-3.4 × 10^38 到 +3.4 × 10^38

 double

 System.Double

 64-bit double-precision floating point

 15/16

 ±5.0 × 10^−324 到 ±1.7 × 10^308

decimal

System.Decimal

128-bit high precision decimal notation

28

(-7.9 x 10^28 到 7.9 x 10^28) / (10^0 ~ 10^28)

在代码中写一个12.3,编译器会自动认为这个数是个double型。想指定12.3为float类型,那么你必须在数字后面加上 f 或 F:float f = 12.3F; decimal类型用后缀 m 或 M:decimal d = 12.30M;

decimal类型用来表示高精度的浮点数,有效位数达到了28位,但是表示的数据范围却比较小。decimal类型并不是C#中的基础类型,所以使用的时候会对计算时的性能有影响。

 

注意:

在精确计算中浮点数精度损失的问题,浮点数的精度损失可能在很多地方出现,例如d * g / g 不一定等于d,d / g * g也不一定等于d。   

所有的浮点型变量都存在精度损失的问题,decimal也是浮点型也存在精度损失,例如

decimal dd = 10000000000000000000000000000m;

dd += 0.1m;

Console.WriteLine ( "{0:G50}", dd );  

输出还是10000000000000000000000000000。

 

 

 

 

*****************************************************
*** No matter how far you go, looking back is also necessary. ***
*****************************************************
posted @ 2018-08-02 16:19  Gangle  阅读(491)  评论(0编辑  收藏  举报