How can I remove ".0" of float numbers?
Asked Answered
M

7

11

Say I have a float number. If it is an integer (e.g. 1.0, 9.0, 36.0), I want to remove the ".0 (the decimal point and zero)" and write to stdout. For example, the result will be 1, 9, 36. If the float number is a rational number such as 2.5335 and 95.5893, I want the output to be as it was inputted, meaning 2.5335 and 95.5893. Is there a smart way to do this?

Only whether the number has .0 or other digits at decimal places does matter. I can say my question is this: how can I know whether or not a float number is actually an integer?

I want to use this to decide directory names. For example, if I input 9.0 and 2.45, make two directories, dir9 and dir2.45.

Misconceive answered 9/7, 2016 at 14:12 Comment(6)
"if the float number is a rational number such as 2.5335 and 95.5893, I want the output to be as it was inputted." What do you mean by this?Isometrics
Possible duplicate of Python: Remove division decimalCopperas
Do you trust Python is correct when it tells you a certain 1.0 does not have zeroes all the way, or do you want to check only the first few digits, as in your examples?Devoted
Are you aware that 0.1 + 0.2 != 0.3 (and other fun consequences of floats being binary rather than decimal)?Xenolith
I understand 0.1 may not be exactly 0.1, but why cannot 1.0 be precisely 1.0? That 0.1 + 0.2 != 0.3 does not seem to be relevant to this question. Could you explain how it is related to my question?Misconceive
You have to be more clear about the phrase "I want the output to be as it was inputted", assuming that the input is a decimal rational number. For instance, 123456789.123456769 == 123456789.123456783 is true for my installation (both these floats are printed as 123456789.12345678). On another note, for large "integer" floats, you'll lose digits before the decimal point (for my installation, int(123456789123456789.0) - 123456789123456789 == -5).Xenolith
F
26

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

Fidole answered 9/7, 2016 at 14:48 Comment(0)
T
11

you can use string formatting

>>> "%g" % 1.1
'1.1'
>>> "%g" % 1.0
'1'
Tristan answered 14/5, 2021 at 16:28 Comment(0)
P
8

You can combine the 2 previous answers :

formatNumber = lambda n: n if n%1 else int(n)
>>>formatNumber(5)
5

>>>formatNumber(5.23)
5.23

>>>formatNumber(6.0)
6
Prelature answered 17/11, 2019 at 13:43 Comment(1)
It's possible to define formatNumber = lambda n: int(n) if isinstance(n,float) and n.is_integer() else nByers
C
8

You can do that with fstrings like

print(f'{1.0:g},{1.2:g}')  # Output: 1,1.2
Clapp answered 24/11, 2021 at 22:34 Comment(0)
B
1
num = 5.0
if float(num) == int(num):
    num = int(num)

Output: 5

Barbet answered 26/4, 2022 at 10:39 Comment(1)
This works, but I think it would be better to write this as a function.Lasseter
A
0

Further to 'MD. Ferdous Ibne Abu Bakar''s answer, should you have a string this function would work:

val = str(5.0)
def remove_zero_float(val):
    if float(val) == int(float(val)):
        val = int(float(val))
    return val

Output: 5

Apotropaic answered 19/5 at 11:38 Comment(0)
B
-4

just type int(number) example :

int(3.0)

returns

3
Bard answered 17/8, 2018 at 12:40 Comment(1)
unfortunately, int(3.5) also returns 3 which the OP specifically does not want to have happen.Histrionic

© 2022 - 2024 — McMap. All rights reserved.