Reputation: 3860
I have a interger value like:
99990
I want this value to be converted into like:
999.90
Regardless of the length of the number, I need to display the number with 2 decimal places at the end, and I'd like to do it in an efficient way. The purpose is to display money amount.
Upvotes: 0
Views: 2736
Reputation:
You can use format
here:
>>> n = 99990
>>> format(n / 100, '.02f')
'999.90'
>>>
The result needs to be a string because Python automatically removes the trailing 0
with numbers:
>>> n = 99990
>>> n / 100
999.9
>>>
For those who are still using Python 2.x, you will need to divide the number by a float:
>>> n = 99990
>>> format(n / 100.0, '.02f')
'999.90'
>>>
Upvotes: 1
Reputation: 58
Dividing with a float 100. value should covert it into a float. After that, it's only a matter of formatting to display it the way you like.
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> number = 99990
>>> print("{0:.2f}".format(number / 100.))
999.90
Upvotes: 0
Reputation: 6935
This will work quickly and easily.
print "%.2f" % (99990 / 100.0)
Upvotes: 0
Reputation: 530922
With an integer value, you can use divmod
to produce separate dollar and cent components.
dollars, cents = divmod(value, 100)
print("{0}.{1}".format(dollars, cents))
Upvotes: 4
Reputation: 707
word = 99990
wordString = str(word)[0:-2] + "." + str(word)[-2:]
print(wordString)
Here's a little code snippet that will word, it converts the number to the string
Upvotes: -1