17

Possible Duplicate:
python limiting floats to two decimal points

i want to set 39.54484700000000 to 39.54 using python ,

how to get it ,

thanks

Community
  • 1
  • 1
zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • 2
    What do you mean by "change"? From your question, we can't guess. Both could strings. Then you're doing a string slice. Both could be float, so you're truncating. The first could be float and the second string and you're formatting. The first could be string and the second float, in which case you're converting and rounding. This question cannot be answered in this form. Fix it, please. – S.Lott Mar 05 '11 at 13:03

6 Answers6

30

If you want to change the actual value, use round as Eli suggested. However for many values and certain versions of Python this will not result be represented as the string "39.54". If you want to just round it to produce a string to display to the user, you can do

>>> print "%.2f" % (39.54484700000000)
39.54

or in newer versions of Python

>>> print("{:.2f}".format(39.54484700000000))
39.54

or with the fstrings

>>> print(f'{39.54484700000000:.2f}')
39.54

Relevant Documentation: String Formatting Operations, Built-in Functions: round

WhoisMatt
  • 103
  • 1
  • 5
Jeremy
  • 1
  • 85
  • 340
  • 366
  • but i dont want get the string '39.54' , i want get the `number` 39.54 – zjm1126 Mar 05 '11 at 07:34
  • 3
    The way that an ordinary number is stored in Python cannot accurately represent the value 39.54 (because it is a base-10 value that does not have a finite base-2 representation). You can either use the decimal.Decimal class as suggested by others, or upgrade to a version of Python which converts the imprecise value to the string that you want (CPython2.7+, IIRC). – Jeremy Mar 05 '11 at 07:51
  • first of all, thanks for posting links to the actual documention. however, i cant seem to find anything regarding the `'{:.2f}'.format(n)`, specifically the `:.` syntax in the documentation? what does the `:.` do exactly and can you point me to the part of the documentation that touches on this? – oldboy Feb 24 '19 at 02:54
3

How about round

>>> import decimal
>>> d=decimal.Decimal("39.54484700000000")
>>> round(d,2)
39.54
the wolf
  • 34,510
  • 13
  • 53
  • 71
3

You can use the quantize method if you're using a Decimal:

In [24]: q = Decimal('0.00')

In [25]: d = Decimal("115.79341800000000")

In [26]: d.quantize(q)
Out[26]: Decimal("115.79")
ars
  • 120,335
  • 23
  • 147
  • 134
2

Use round:

Return the floating point value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done away from 0

>>> round(39.544847, 2)
39.539999999999999
>>> 

Note that since 39.54 isn't exactly represantable with floating points on my PC (x86), the result is an epsilon off. But that makes no difference (and is a whole different issue with many SO questions and answers on it). If you convert it to a string properly, you'll see what you expect:

>>> "%.2f" % round(39.544847, 2)
'39.54'
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
2
>>> round(39.54484700000000, 2)
39.54

Note, however, that the result isn't actually 39.54, but 39.53999999999999914734871708787977695465087890625.

dan04
  • 87,747
  • 23
  • 163
  • 198
2

Eli mentions using the round function -- depending on your requirements, you may want to return a Decimal object instead.

>>> from decimal import Decimal
>>> float_val = 39.54484700000000
>>> decimal_val = Decimal("%.2f" % float_val)
>>> print decimal_val
39.54

Using Decimal objects lets you specify the exact number of decimal places that you want to keep track of, so you avoid ending up with a floating point number that is represented as 39.539999999999999. Specifically, if you are doing financial calculations, you will almost always be advised to stay away from floating-point numbers.

You can't cast floats directly into Decimals, however (the floats are imprecise, and Python can't guess how you want them rounded,) so I will almost always convert them to a rounded string representation first (that's the "%.2f" % float_val -- %.2f means to display only two decimals, and then create a Decimal out of that.

Ian Clelland
  • 43,011
  • 8
  • 86
  • 87