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).
Videos
Hi all, I have a string a = '1721244344.700249000', I want to convert it to a floating value.
Float() is returning only 2 places after decimal point. Like 1721244344.7
Is there a way I can convert the entire string to a floating point value and get all the decimal (upto 9 places after decimal point)?
I have to use python v2.7 for this.
Edit: I do not have problem in printing the all 9 decimal places but I need the floating value so that I can subtract another value so that I get the difference with accuracy upto 9 th decimal point.
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 :)
You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.
With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).
Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.
For example,
>>> 125650429603636838/(2**53)
13.949999999999999
>>> 234042163/(2**24)
13.949999988079071
>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999
If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:
- Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
- Or use a fixed point number like decimal.
There are new format specifications, String Format Specification Mini-Language:
You can do the same as:
"{:.2f}".format(13.949999999999999)
Note 1: the above returns a string. In order to get as float, simply wrap with float(...):
float("{:.2f}".format(13.949999999999999))
Note 2: wrapping with float() doesn't change anything:
>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
For example if i input 25$ i want the output to be 25.00 Or input 50% output 0.50