• 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. ***
    *****************************************************
  • 相关阅读:
    打开服务器的文档
    笔记
    centos6.5 编译openssl-1.1.1k
    搭建自己的低代码平台
    防火墙ACL配置自动化
    防火墙ACL配置自动化
    【树莓派】读取新大陆(newland)USB条码扫描器数据
    解决eclipse或sts闪退的办法(转)
    浅谈数据库迁移类项目功能测试的基本思路
    ATM取款机优化需求的用例设计
  • 原文地址:https://www.cnblogs.com/gangle/p/9407858.html
Copyright © 2020-2023  润新知