C#中的除法结果不精确到小数点后的值 [英] Division in C# results not exact value after decimal point

查看:386
本文介绍了C#中的除法结果不精确到小数点后的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下划分显示错误结果。根据Windows计算器,它应为15位数字。但是在C#中显示14位数字。

Following division showing incorrect result. It should be 15 digits after point according to the windows calculator. But in C# showing 14 digits after point.

推荐答案

如果数字必须正确加总或保持平衡,使用十进制。这包括人们可能手工进行的任何财务存储或计算,分数或其他数字。

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

如果数字的确切值并不重要,请使用double表示速度。这包括图形,物理学或其他物理科学计算,其中已经有有效位数。

If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits".

请参考下面的代码以获取更多帮助。

Please refer to below code for any further help.

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

这篇关于C#中的除法结果不精确到小数点后的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆