How to set precision without rounding?

Nips

I have big integers in database (numeric(24,0)). And I want to convert this number to "human readable" format. I have function:

def from_int(value, precision):
    fprec = "%." + str(precision) + "f"
    return fprec % (Decimal(value) * (Decimal(10)**(-1*precision)))

And it works:

from_int(1000000, 6)
'1.000000'
from_int(1000000, 8)
'0.01000000'
from_int(1000000, 12)
'0.000001000000'
from_int(1000000, 2)
'10000.00'

but for:

from_int(19999999999999999, 2)
'200000000000000.00'

How to set precision without rounding?

Janne Karila

Formatting with %f converts the number to floating point losing precision. Instead use str:

def from_int(value, precision):
    return str(Decimal(value) * Decimal(10)**(-precision))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set the decimal precision globally in python without always typing Decimal()?

From Dev

convert number string into float with specific precision (without getting rounding errors)

From Dev

Preventing MATLAB from rounding numbers and set decimal precision

From Dev

How to truncate a BigDecimal without rounding

From Dev

Lodash rounding precision

From Dev

Rounding to a fixed relative precision

From Dev

Clojure - How to set the lower precision

From Dev

How to convert string to number in php without rounding?

From Dev

Python - How to remove decimals without rounding up

From Dev

How to convert string to currency without rounding up?

From Dev

Rounding and trimming digits with specified precision

From Dev

How to set precision and scale in ALTER TABLE

From Dev

How to properly set precision for doubles in C++

From Dev

How to set precision and scale in ALTER TABLE

From Dev

How to set specific precision to BigDecimal in Java?

From Dev

How to set the precision on a SAS numeric value

From Dev

Format decimal number with set maximal precision but without unneeded trailing zeroes

From Dev

Possibly to set float digit precision in display string dynamically without NSNumberFormatter?

From Dev

How to display a fixed number of digits in C++ without rounding

From Dev

How do I format double input in Java WITHOUT rounding it?

From Dev

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

From Dev

How to truncate the decimal values of a floating point number without rounding?

From Dev

How to convert number from double to int without rounding?

From Dev

How to restrict the decimal places in awk without rounding off

From Dev

How do I cut off a decimal without rounding?

From Dev

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

From Dev

How to write string with 3 significant digits without rounding

From Dev

Precision, Rounding, and data types in XSLT2.0

From Dev

PHP incorrect precision while rounding off number

Related Related

  1. 1

    How to set the decimal precision globally in python without always typing Decimal()?

  2. 2

    convert number string into float with specific precision (without getting rounding errors)

  3. 3

    Preventing MATLAB from rounding numbers and set decimal precision

  4. 4

    How to truncate a BigDecimal without rounding

  5. 5

    Lodash rounding precision

  6. 6

    Rounding to a fixed relative precision

  7. 7

    Clojure - How to set the lower precision

  8. 8

    How to convert string to number in php without rounding?

  9. 9

    Python - How to remove decimals without rounding up

  10. 10

    How to convert string to currency without rounding up?

  11. 11

    Rounding and trimming digits with specified precision

  12. 12

    How to set precision and scale in ALTER TABLE

  13. 13

    How to properly set precision for doubles in C++

  14. 14

    How to set precision and scale in ALTER TABLE

  15. 15

    How to set specific precision to BigDecimal in Java?

  16. 16

    How to set the precision on a SAS numeric value

  17. 17

    Format decimal number with set maximal precision but without unneeded trailing zeroes

  18. 18

    Possibly to set float digit precision in display string dynamically without NSNumberFormatter?

  19. 19

    How to display a fixed number of digits in C++ without rounding

  20. 20

    How do I format double input in Java WITHOUT rounding it?

  21. 21

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

  22. 22

    How to truncate the decimal values of a floating point number without rounding?

  23. 23

    How to convert number from double to int without rounding?

  24. 24

    How to restrict the decimal places in awk without rounding off

  25. 25

    How do I cut off a decimal without rounding?

  26. 26

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

  27. 27

    How to write string with 3 significant digits without rounding

  28. 28

    Precision, Rounding, and data types in XSLT2.0

  29. 29

    PHP incorrect precision while rounding off number

HotTag

Archive