Getting only 1 decimal place [duplicate] - python

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)

Related

Reducing floats to two decimal points [duplicate]

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

how to sum to large string number in python [duplicate]

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...

Python: How do i Remove a CERTAIN amount of decimal in a float [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 2 years ago.
so i had this problem where i generated numbers from 0-1 with a step of 0.01 but the numbers have so many decimals like 0.010101010101.I only need it in the form 0.01 with two decimals.
How do i remove the rest of decimals ?
You can format the decimal like
your_value = 0.1010101010
desired_value = '%.2f' % your_value
Now desired_value will have the decimal upto 2 places.
Or you could use decimal.
Look at the documentation in the link:
https://docs.python.org/3/library/decimal.html
Round num to digits places. round(num, digits)

Python Converts Float Incorrectly [duplicate]

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

Calc correct result of 100.1+0.1 as string [duplicate]

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'

Categories

Resources