You are confusing float values with their string representation. float(3) is enough, and whenever you need to print one, use formatting.
va = float('3')
print format(va, '.2f')
print isinstance(va, float)
float objects themselves have no concept of a number of decimal places to track.
You are confusing float values with their string representation. float(3) is enough, and whenever you need to print one, use formatting.
va = float('3')
print format(va, '.2f')
print isinstance(va, float)
float objects themselves have no concept of a number of decimal places to track.
Just use float("3") to achieve that but notice that a float does not have a specific number of digits after the decimal point; that's more a feature of outputting a float using string formatting. So you can use '%.2f' % float("3") to see your float value with two decimal digits.
Your tests were all flawed in several aspects.
va = '%.2f' % float('3') created a str which looked like a float, not a float.
vb = float('%.2f' % float('3')) created a decent float but your printing test print vb then did not format the float to using two decimal digits after the point. It just used the default formatting (which prints one trailing .0 to make clear that this is not an int).
How to format to two decimal places python
python - Convert plain string number to decimal with 2 places - Stack Overflow
python - How can I format a decimal to always show 2 decimal places? - Stack Overflow
python - How to display a float with two decimal places? - Stack Overflow
Videos
I want to format a float so that it will round to two decimal places, but I am not sure how to do that. Can someone help me? I tried using round() but it doesn't work.
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'
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'
Since this post might be here for a while, lets also point out python 3 syntax:
"{:.2f}".format(5)
You could use the string formatting operator for that:
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
The result of the operator is a string, so you can store it in a variable, print etc.
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 :)