【问题标题】:Division in C# results not exact value after decimal pointC# 中的除法结果不是小数点后的精确值
【发布时间】:2016-11-28 16:36:32
【问题描述】:

以下除法显示不正确的结果。根据windows计算器应该是点后15位。但在 C# 中,点后显示 14 位数字。

【问题讨论】:

  • 我正在使用 Visual Studio 2010
  • 我也试过 (Double)200/(Double)30 但显示相同的结果。

标签: c#-4.0


【解决方案1】:

我修好了。更多的比例位数将是(十进制)200/(十进制)30

【讨论】:

    【解决方案2】:

    如果数字必须正确相加或平衡,请使用小数。这包括人们可能手动进行的任何财务存储或计算、分数或其他数字。

    如果数字的确切值不重要,请使用 double 来提高速度。这包括已经存在“有效数字位数”的图形、物理或其他物理科学计算。

    请参考以下代码以获得更多帮助。

    C# Type .Net Framework (System) type    Signed? Bytes Occupied  Possible Values
        sbyte   System.Sbyte    Yes 1   -128 to 127
        short   System.Int16    Yes 2   -32768 to 32767
        int System.Int32    Yes 4   -2147483648 to 2147483647
        long    System.Int64    Yes 8   -9223372036854775808 to 9223372036854775807
        byte    System.Byte No  1   0 to 255
        ushort  System.Uint16   No  2   0 to 65535
        uint    System.UInt32   No  4   0 to 4294967295
        ulong   System.Uint64   No  8   0 to 18446744073709551615
        float   System.Single   Yes 4   Approximately ±1.5 x 10-45 to ±3.4 x 1038 with 7 significant figures
        double  System.Double   Yes 8   Approximately ±5.0 x 10-324 to ±1.7 x 10308 with 15 or 16 significant figures
        decimal System.Decimal  Yes 12  Approximately ±1.0 x 10-28 to ±7.9 x 1028 with 28 or 29 significant figures
        char    System.Char N/A 2   Any Unicode character (16 bit)
        bool    System.Boolean  N/A 1 / 2   true or false
    

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      相关资源
      最近更新 更多