Rounding to specfic digits fails with this double-precision value

Robin Rodricks

I'm attempting to truncate a series of double-precision values in C#. The following value fails no matter what rounding method I use. What is wrong with this value that causes both of these methods to fail? Why does even Math.Round fail to correctly truncate the number? What method can be used instead to correctly truncate such values?

The value :

double value = 0.61740451388888251;

Method 1:

return Math.Round(value, digits);

Method 2:

double multiplier = Math.Pow(10, decimals)
return Math.Round(value * multiplier) / multiplier;

Fails even in VS watch window!

round

PiotrWolkowski

Double is a floating binary point type. They are represented in binary system (like 11010.00110). When double is presented in decimal system it is only an approximation as not all binary numbers have exact representation in decimal system. Try for example this operation:

double d = 3.65d + 0.05d;

It will not result in 3.7 but in 3.6999999999999997. It is because the variable contains a closest available double.

The same happens in your case. Your variable contains closest available double.

For precise operations double/float is not the most fortunate choice. Use double/float when you need fast performance or you want to operate on larger range of numbers, but where high precision is not required. For instance, it is perfect type for calculations in physics. For precise decimal operations use, well, decimal.

Here is an article about float/decimal: http://csharpindepth.com/Articles/General/FloatingPoint.aspx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rounding to specfic digits fails with this double-precision value

From Dev

Rounding and trimming digits with specified precision

From Dev

Double to Decimal without rounding after 15 digits

From Dev

Rounding floating variables in C to display upto only 2 digits of precision

From Dev

rounding of digits

From Dev

rounding of digits

From Dev

Precision loss / rounding difference when directly assigning double result to an int

From Java

String.format() rounding a double with leading zeros in digits - Java

From Dev

How to truncate a double to four digits in hive without rounding?

From Dev

How to truncate a double to four digits in hive without rounding?

From Dev

Double value with specific precision in java

From Dev

Format the double precision value in Postgresql

From Dev

c++ long double printing all digits with precision

From Dev

\(Double) rounding

From Dev

double rounding

From Dev

Double precision value computation errors on MediaTek processors

From Dev

Double precision value computation errors on MediaTek processors

From Dev

Lodash rounding precision

From Dev

Rounding to a fixed relative precision

From Dev

printf a float value with precision (number of decimal digits) passed in a variable

From Dev

Rounding to n significant digits

From Dev

Rounding digits in ggpairs

From Dev

Round double value to 2 decimal digits

From Java

Rounding a double value to x number of decimal places in swift

From Dev

How to test if value stored in double fit in long? (rounding yes, but truncating no)

From Dev

PostgreSQL - get all integer digits from double precision field for use in to_char function

From Dev

How to set precision without rounding?

From Dev

Rounding to specific number of digits in Haskell

From Dev

Rounding to specific number of digits in Haskell

Related Related

  1. 1

    Rounding to specfic digits fails with this double-precision value

  2. 2

    Rounding and trimming digits with specified precision

  3. 3

    Double to Decimal without rounding after 15 digits

  4. 4

    Rounding floating variables in C to display upto only 2 digits of precision

  5. 5

    rounding of digits

  6. 6

    rounding of digits

  7. 7

    Precision loss / rounding difference when directly assigning double result to an int

  8. 8

    String.format() rounding a double with leading zeros in digits - Java

  9. 9

    How to truncate a double to four digits in hive without rounding?

  10. 10

    How to truncate a double to four digits in hive without rounding?

  11. 11

    Double value with specific precision in java

  12. 12

    Format the double precision value in Postgresql

  13. 13

    c++ long double printing all digits with precision

  14. 14

    \(Double) rounding

  15. 15

    double rounding

  16. 16

    Double precision value computation errors on MediaTek processors

  17. 17

    Double precision value computation errors on MediaTek processors

  18. 18

    Lodash rounding precision

  19. 19

    Rounding to a fixed relative precision

  20. 20

    printf a float value with precision (number of decimal digits) passed in a variable

  21. 21

    Rounding to n significant digits

  22. 22

    Rounding digits in ggpairs

  23. 23

    Round double value to 2 decimal digits

  24. 24

    Rounding a double value to x number of decimal places in swift

  25. 25

    How to test if value stored in double fit in long? (rounding yes, but truncating no)

  26. 26

    PostgreSQL - get all integer digits from double precision field for use in to_char function

  27. 27

    How to set precision without rounding?

  28. 28

    Rounding to specific number of digits in Haskell

  29. 29

    Rounding to specific number of digits in Haskell

HotTag

Archive