You could use %g to achieve this:
'%g'%(3.140)
or, with Python โฅ 2.6:
'{0:g}'.format(3.140)
or, with Python โฅ 3.6:
f'{3.140:g}'
From the docs for format: g causes (among other things)
Answer from unutbu on Stack Overflowinsignificant trailing zeros [to be] removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
You could use %g to achieve this:
'%g'%(3.140)
or, with Python โฅ 2.6:
'{0:g}'.format(3.140)
or, with Python โฅ 3.6:
f'{3.140:g}'
From the docs for format: g causes (among other things)
insignificant trailing zeros [to be] removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Me, I'd do ('%f' % x).rstrip('0').rstrip('.') -- guarantees fixed-point formatting rather than scientific notation, etc etc. Yeah, not as slick and elegant as %g, but, it works (and I don't know how to force %g to never use scientific notation;-).
Hello everyone,
I am still new to python and learning.
So I practiced some exercises and made an app that calculates the percentage from the number the user enters.
My question use, how can I terminate the .0 part if the user enters an Int and keep the decimal part if they enter a float?
so for example, 5% of 100 is 5 ( Int)
and 5.1% of 100 is 5.1 (float)
General way to print floats without the .0 part
How to eliminate trailing zeros?
python - Inteligently remove ".0" from float output - Stack Overflow
formatting - remove trailing zeros in float representation python - Stack Overflow
Videos
Here's a function to format your numbers the way you want them:
def formatNumber(num):
if num % 1 == 0:
return int(num)
else:
return num
For example:
formatNumber(3.11111)
returns
3.11111
formatNumber(3.0)
returns
3
you can use string formatting
>>> "%g" % 1.1
'1.1'
>>> "%g" % 1.0
'1'
I have a float formatted to 2 decimal places. I need to eliminate the 2nd decimal place if it's a "0" but still keep 2 decimal places open for when its 2 whole numbers.
number = float(25.20458)
print(format(number, ".2f"))
#Comes out as 25.20
#Need 25.2Windows 10 and Python 3.7
Format your number with .15g
>>> format(555.123, '.15g')
555.123
>>> format(5.0, '.15g')
5
Though it will use the scientific exponent format for numbers close to zero:
>>> format(0.00001, '.16g')
1e-05
and for numbers that have 16+ digits before decimal point.
Note that you do not need to use the '{}'.format(); format built-in function as above works better here.
If you want to convert only float objects that represent integers to int (i.e. convert 9.0 to 9 but leave 9.5 as it is), you can use float.is_integer to check:
>>> numbers = [1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
>>> numbers = map(lambda f: int(f) if f.is_integer() else f, numbers)
>>> numbers
[1, 1.2, 1.4, 1.6, 1.8, 2]
>>> map(type, numbers)
[<type 'int'>, <type 'float'>, <type 'float'>, <type 'float'>, <type 'float'>, <type 'int'>]
Alternatively, if you want to apply the conversion to the string (i.e. without converting the JSON to Python objects), you could use a regular expression (see demo):
>>> import re
>>> data = """
{
"id": "7418",
"name": "7.5"
},
{
"id": "7419",
"name": "8.0"
}, """
>>> print re.sub(r'"(\d+)\.0"', r'"\1"', data)
{
"id": "7418",
"name": "7.5"
},
{
"id": "7419",
"name": "8"
},
Note again that "7.5" is untouched, but "8.0" is replaced with "8".