This question already has answers here:
Python 3 Float Decimal Points/Precision [duplicate]
(5 answers)
Closed 7 years ago.
I have digits, for example:
100.1264
9.09
123
1298.456789
and I want to truncate this to:
100.12
9.09
123
1298.45
How to truncate those numbers so as to leave their form?
If you have numbers (not strings), you can use:
import math
math.trunc(x * 100) / 100
For example:
>>> import math
>>> x = 100.1264
>>> math.trunc(x * 100) / 100
100.12
You may also use int in place of math.trunc, however beware that casts between floats and ints may be computationally expensive.
Bonus tip: if you want arbitrary precision decimal arithmetic, take a look at the decimal module.
s="""100.1264
9.09
123
343.1
1298.456789"""
print re.sub(r"(?<=\.)(\d{2})\d+",r"\1",s)
If your input is a float you can use
s=100.1264
print ast.literal_eval(re.sub(r"(?<=\.)(\d{2})\d+",r"\1",str(s)))
Related
This question already has answers here:
How can I format a decimal to always show 2 decimal places?
(13 answers)
Closed 3 months ago.
i want to round off a float to 3 dp in python with 00 in the end if the float don't have 3 dp
like 15.4 into 15.400
thank you.
programme:
x=round(15.4)
result:
15.400
The "rounding" you are talking about can only be done if you convert the float to a string. This is usually only done for display purposes. In this case you can use a so-called f-string to do this formatting:
x = 15.4
print(f"{x:.3f}")
Hello its pretty simple you can do something like this
a=15.4
b=("%.3f" % a)
print(b)
15.4 and 15.400 are the same number. round() returns a number. What you want is to have a different representation when you convert it to a string.
You need to do string formatting. Just copying the other answers here, there are two ways.
f-strings:
n = 15.4
n_str = f"{n:.3f}"
%-formatting:
n_str = "%.3f" % n
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:
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:
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.