This question already has answers here:
Convert floating point number to a certain precision, and then copy to string
(7 answers)
Closed 5 years ago.
Using %-formatting, I can specify the number of decimal cases in a string:
x = 3.14159265
print('pi = %0.2f' %x)
This would give me:
pi = 3.14
Is there any way of doing this using f-strings in Python 3.6?
How about this
x = 3.14159265
print(f'pi = {x:.2f}')
Docs for f-strings
Related
This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am trying to divide floats by each other but am having a hard time getting accurate results. I understand that computers store floats in a way where the value stored is not exact to the given number. I am simply looking for a way where I can get specific results when working with floats.
input:
x = 2.4
y = 0.2
print(x/y)
Output:
11.999999998
I highly recommend to use decimals
Example
from decimal import Decimal
x = Decimal("2.4")
y = Decimal("0.2")
print(x / y) # 12
Notice we passing number as string, as passing float numbers would have the same problem you pointed out.
But care with comparison, as 12 == x / y evaluates to False
This question already has an answer here:
Is Python's Decimal class variable width?
(1 answer)
Closed 1 year ago.
How to sum these two large strings, if I transform to FLOAT it loses the accuracy.
str1= '5.123654879542658'
str2= '8.777548795426584'
str(float(string1) + float(string2))
The decimal package is for high precision (28 places by default) maths:
>>> from decimal import Decimal
>>> str1= '5.123654879542658'
>>> str2= '8.777548795426584'
>>> Decimal(str1) + Decimal(str2)
Decimal('13.901203674969242')
Update: #KarlKnechtel seems to have found a close duplicate, oh well...
This question already has answers here:
Fixed digits after decimal with f-strings
(8 answers)
Closed 2 years ago.
I am writing confidence level using following code into my Image
text1="color is {}".format(f'Class #{i + 1}-{class_names[ind]}-Confidence: {100 * conf}%')
how do I limit my confidence number to two decimal places rather than the 10 or so it gives?
f'{100 * conf:.2f}' has to work
This question already has answers here:
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 8 years ago.
If i calc result of some float samples
>>> 100.1+0.1
100.19999999999999
how to get correct string "100.2"?
I use Py3.2 / Win.
Use decimal.Decimal to do decimal floating point arithmetic correctly:
>>> import decimal
>>> decimal.Decimal('100.1') + decimal.Decimal('0.1')
Decimal('100.2')
>>> str(decimal.Decimal('100.1') + decimal.Decimal('0.1'))
'100.2'
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 6 years ago.
How do I convert 45.34531 to 45.3?
Are you trying to represent it with only one digit:
print("{:.1f}".format(number)) # Python3
print "%.1f" % number # Python2
or actually round off the other decimal places?
round(number,1)
or even round strictly down?
math.floor(number*10)/10
>>> "{:.1f}".format(45.34531)
'45.3'
Or use the builtin round:
>>> round(45.34531, 1)
45.299999999999997
round(number, 1)