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...
Related
This question already has answers here:
Python rounding error with float numbers [duplicate]
(2 answers)
Closed 6 years ago.
Can someone tell me what I'm missing here? This is using Python 2.7.11:
print float(148.95)
print float(148.95)*100
print int(float(148.95)*100)
Why does this print:
148.95
14895.0
14894 <--- Shouldn't this be 14895?
148.95 is not a number that can be exactly represented using floating point. The number internally stored is actually 148.94999999999998863131622783839702606201171875. When you multiply by a hundred, you get 14894.999999999998181010596454143524169921875. When you convert that to integer, it cuts off the .999... and you're left with 14894.
If you want a data type that can exactly represent numbers with at least two decimal places of precision, consider using Decimal.
>>> from decimal import Decimal
>>> x = Decimal("148.95")
>>> print x
148.95
>>> print x*100
14895.00
>>> print int(x*100)
14895
This question already has answers here:
How to print float to n decimal places including trailing 0s?
(5 answers)
Closed 7 years ago.
I am approximating phi to 50 decimal places and want to return the value I have computed. I do not want to print it otherwise I can't use it in calculations.
How can I do this because python only wants to display 11 decimal places or something like that?
Thanks
python floats do not have that precision. you need to use python decimal:
from decimal import getcontext, Decimal
getcontext().prec = 50
print(Decimal(1) / Decimal(7))
the drawback is that calculations with these will take much more time than the ones with float.
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:
Print floating point values without leading zero
(13 answers)
Closed 8 years ago.
I have a simple question, that I feel should have a simple solution. How do I format a floating number so that only the numbers after the decimal point show? I would prefer to use '{}'.format to accomplish this.
>>> n = 0.12345
>>> n
0.12345
>>> str(n)[1:]
'.12345'
>>> '{}'.format(n)
'0.12345'
>>> '{}'.format(str(n)[1:])
'.12345'
I know I can use str(n)[1:], but I'd prefer not to have to convert the number to a string.
I do not think there is a format string which removes the zero. However, you could use lstrip:
In [25]: n = 0.12345
In [26]: '{:.3f}'.format(n).lstrip('0')
Out[26]: '.123'
At least that is safer than str(n)[1:], which would remove a signficiant digit if n were equal to a number bigger than 1 or less than -1.
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)