You should use the new format specifications to define how your value should be represented:
>>> from math import pi # pi ~ 3.141592653589793
>>> '{0:.2f}'.format(pi)
'3.14'
The documentation can be a bit obtuse at times, so I recommend the following, easier readable references:
- the Python String Format Cookbook: shows examples of the new-style
.format()string formatting - pyformat.info: compares the old-style
%string formatting with the new-style.format()string formatting
Python 3.6 introduced literal string interpolation (also known as f-strings) so now you can write the above even more succinct as:
>>> f'{pi:.2f}'
'3.14'
Answer from BioGeek on Stack OverflowYou should use the new format specifications to define how your value should be represented:
>>> from math import pi # pi ~ 3.141592653589793
>>> '{0:.2f}'.format(pi)
'3.14'
The documentation can be a bit obtuse at times, so I recommend the following, easier readable references:
- the Python String Format Cookbook: shows examples of the new-style
.format()string formatting - pyformat.info: compares the old-style
%string formatting with the new-style.format()string formatting
Python 3.6 introduced literal string interpolation (also known as f-strings) so now you can write the above even more succinct as:
>>> f'{pi:.2f}'
'3.14'
The String Formatting Operations section of the Python documentation contains the answer you're looking for. In short:
"%0.2f" % (num,)
Some examples:
>>> "%0.2f" % 10
'10.00'
>>> "%0.2f" % 1000
'1000.00'
>>> "%0.2f" % 10.1
'10.10'
>>> "%0.2f" % 10.120
'10.12'
>>> "%0.2f" % 10.126
'10.13'
Videos
Example:
hourlyRate = 20 hoursLabor = 1.6
I want the answer to show โ32.00โ instead of โ32โ or โ32.0โ.
What I have so far produces the number 32.0:
totalPay = float(hourlyRate * hoursLabor)
print(totalPay)
Iโm obviously very new at this. Just getting some beginnerโs practice :)
I've got a number that needs to be rounded to 2 decimal places. Getting the round is easy, but if the number is a whole number or only has 1 digit beyond the decimal the rounding doesn't properly show 2 decimal places.
answer = 1 print(round(float(answer),2)) >> 1.0
I really like f-strings, so I would use this:
answer = 1
print(f"{round(float(answer),2):.2f}")
>> 1.00
Is there a neater or more readable method of doing this?
Do the second one, just donโt worry about rounding it. Let the formatting do the rounding for display.
Also, formatting to round things is one instance where I sometimes prefer the .format() method to fstrings, as you can define the formatting once and use it again and again:
>>> FORMAT_2DP = "{:.2f}"
>>> numbers = 1, 3, 4, 5
>>> for number in numbers:
... print(FORMAT_2DP.format(number))
...
1.00
3.00
4.00
5.00
You can use the general format types of string formatting.
print('input a number')
chuu = float(input())
print('{:g}'.format(round(chuu, 2)))
you could simply do this
print ('input a number')
chuu = float(input())
chuu = round(chuu,2)
if int(chuu)==chuu:
print(int(chuu))
else:
print(chuu)