0

This is what I have:
x = 2.00001

This is what I need:
x = 2.00

I am using:
float("%.2f" % x)

But all I get is:
2

How can I limit the decimal places to two AND make sure there are always two decimal places even if they are zero?
Note: I do not want the final output to be a string.

Shil
  • 9
  • Where do you want this `two decimal places` ? Internally floating point numbers are stored in binary format, it basically depends on where you want this two decimal places formatted floating point number – Anand S Kumar Aug 07 '15 at 17:49
  • 1
    Hi Shil, what are you using this for? If you need the value, then of course 2.00 == 2. If you need to display it, why can you not convert it to a string? – Ryan O'Donnell Aug 07 '15 at 17:53
  • Are you trying to represent an amount of dollars and cents using a float? This is generally discouraged due to the imprecise nature of floating point numbers. There tend to be [bad consequences](https://en.wikipedia.org/wiki/Office_Space) when money transactions end up being off by a fraction of a cent here or there. – Kevin Aug 07 '15 at 17:59
  • 1
    It doesn't matter how you format it if you're going to call `float()` on it in the end. `2.00` is _exactly the same value_ as `2` or `2.0`. – TigerhawkT3 Aug 07 '15 at 17:59
  • 1
    Maybe use the [`decimal` package](https://docs.python.org/2/library/decimal.html)? – ErikR Aug 07 '15 at 18:04
  • http://stackoverflow.com/questions/1530430/how-do-i-change-my-float-into-a-two-decimal-number-with-a-comma-as-a-decimal-poi – Ayy Aug 07 '15 at 18:06
  • I want to transfer the data to excel (csv file). I need all numbers to be consistent without converting to string. – Shil Aug 11 '15 at 15:12

5 Answers5

0

This works:

'%.2f" % float(x)

Previously I answered with this:

How about this?

def fmt2decimals(x):
  s = str(int(x*100))
  return s[0:-2] + '.' + s[-2:]

AFAIK you can't get trailing zeros with a format specification like %.2f.

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • This would be good if the number were to be stored as a formatted string, but the OP wants it to be stored as a float with a certain number of trailing zeros, which is unfortunately not possible as far as I'm aware. – TigerhawkT3 Aug 07 '15 at 18:00
  • Ok - I guess I understand the question now. – ErikR Aug 07 '15 at 18:01
0

If you can use decimal (https://docs.python.org/2/library/decimal.html) instead of float:

from decimal import Decimal
Decimal('7').quantize(Decimal('.01'))

quantize() specifies where to round to.

https://docs.python.org/2/library/decimal.html#decimal.Decimal.quantize

obvious
  • 31
  • 2
0

Have you taken a look at the decimal module? It allows you to do arithmetic while maintaining the proper precision:

>>> from decimal import Decimal
>>> a = Decimal("2.00")
>>> a * 5
Decimal('10.00')
>>> b = Decimal("0.05")
>>> a * b
Decimal('0.1000')
Jake Griffin
  • 2,014
  • 12
  • 15
0

Python also has a builtin "round" function: x = round(2.00001, 2) I believe is the command you would use.

isosceleswheel
  • 1,516
  • 12
  • 20
  • I am trying to ultimately save the numeric data as a csv file, and I would rather not have quotation marks around the numbers, and keep consistent with number of decimal places at the same time. – Shil Aug 09 '15 at 01:54
  • @Shil That shouldn't be a problem, say you had a list like `list = [2.00001, 3.0134, 1.0]` and you want to write that as a row of CSV. You can just use format and join like this: `','.join(['{0:.2f}'.format(item) for item in list])`. When you write this to a file, there will not be any quotes around the row and it will look like this: `2.00,3.01,1.00` – isosceleswheel Aug 09 '15 at 16:43
  • Thank you, but I do need to keep it in a list format so that I can apply functions like insert(). – Shil Aug 11 '15 at 16:45
  • @Shil my comment was for writing to .csv, which you said you'd ultimately like to do with the data in the list once you are finished with whatever other processing you do – isosceleswheel Aug 11 '15 at 18:31
  • Apparently, `"%.2f" % x` works just fine. It's the excel display format that changes 2.00 to 2. This is because excel's default cell format is 'general'; it should be 'number' in this case. Now I basically have to lookup excel file formatting. Thanks everyone. – Shil Aug 11 '15 at 19:33
0

Well, in Python, you can't really round to two zeroes without the result being a string. Python will usually always round to the first zero because of how floating point integers are stored. You can round to two digits if the second digit is not zero, though.

For example, this:

round(2.00001, 2)
#Output: 2.0

vs this:

round(2.00601, 2)
#Output: 2.01
jyapayne
  • 610
  • 6
  • 13