12

How can truncate an input like 315.15321531321 I want to truncate all the values after the hundredths position so it becomes 315.15

how do i do that?

  • 1
    You might also want to look at http://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points/455634#455634 – Rex Logan Jun 09 '09 at 00:45
  • Rex, I liked the answer at the question you quoted so much, I just upvoted it; you did a great job explaining the connections between int, float and Decimal in Python. – Jarret Hardie Jun 09 '09 at 01:37

8 Answers8

13

String formatting under python 2.x should do it for you:

>>> print '%.2f' % 315.15321531321
315.15

This limits the string representation to just 2 decimal places. Note that if you use round(315.153215, 2), you'll end up with another float value, which is naturally imprecise (or overprecise, depending on how you look at it):

>>> round(315.15321531321, 2)
315.14999999999998

Technically, round() is correct, but it doesn't "truncate" the results as you requested at 315.15. In addition, if you round a value like 315.157, it will produce something closer to 315.16... not sure if that's what you mean by "truncate".

Jarret Hardie
  • 84,200
  • 10
  • 123
  • 121
  • 1
    Interestingly, I read yesterday that Python 3.1rc1 now chooses the shortest string representation that resolves to the same floating point number, so your second example with round() would print 315.15 as expected: http://docs.python.org/dev/py3k/whatsnew/3.1.html#other-language-changes – Greg Hewgill Jun 09 '09 at 00:46
  • Thank you, Greg - I didn't know that. Good additional info; the more I hear about py3k, the more I can't wait to use it day-to-day! – Jarret Hardie Jun 09 '09 at 00:49
9

Looks like print "%.2f" does rounding as well. Here is Python code that rounds and truncates

num = 315.15627
print "rounded   = %.2f" % num
print "truncated = %.2f" % (int(num*100)/float(100))

rounded   = 315.16
truncated = 315.15
rainerpm
  • 153
  • 1
  • 5
6

If you're working with currency amounts, I strongly recommend that you use Python's decimal class instead: http://docs.python.org/library/decimal.html

Dave
  • 9,441
  • 1
  • 35
  • 32
3

If you just want to display it shortened, you can use the "%f" formating flag:

value = 315.123123123
print "Value is: %.2f" % value

If you want to really cut off the "additional" digits, do something like:

from math import trunc
value = trunc(value*100)/100
sth
  • 200,334
  • 49
  • 262
  • 354
1

Maybe this will help you:

a=315.15321531321
a=a*100
a=int(a)
a=a/100.0
print(a)
315.15

Explanation: If you want to round at the second decimal place then you should:
1. multiply your number by 100; this gives you 31515.321531321,
2. convert this new number to integer; int(31515.321531321) gives you 31515,
3. divide this by 100.0; 31515/100.0,
4. and you get the correct answer; 315.15

Enjoy!

eod
  • 316
  • 1
  • 12
  • Your answer certainly is worth a little explanation. Kindly refer to http://stackoverflow.com/help/how-to-answer . – J. Chomel Mar 02 '17 at 17:04
1

Built-in function round():

>>> num = 315.1532153132
>>> round(num, 2)
3.1499999999999998
Triptych
  • 188,472
  • 32
  • 145
  • 168
1

You have several options - you can round the number using round(), however this can introduce some inaccuracies (315.15 might round to 315.150000003 for example). If you're just looking to truncate the value of the float when you're displaying it, you can specify the width of the output using printf("%.2f", mynumber). This is probably a better solution, since without knowing more about your specific application it's a good idea in general to keep the entire length of the number for calculation.

Zxaos
  • 6,981
  • 11
  • 44
  • 59
0

When working with Decimal types and currency, where the amount needs to be precise, here is what I came up with for truncating to cents (hundredths):

amount = Decimal(long(amount * 100)) / Decimal(100)

A little history: I tried quantize but it tends to round rather than truncate. And I couldn't in good conscience use expensive string formatters for something so simple. And finally I didn't want any precision problems a la float so I use very specific casting that is probably overkill.

Neil C. Obremski
  • 15,617
  • 20
  • 62
  • 90