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.

Answer from unutbu on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to terminate or remove the .0 point from an int.
r/learnpython on Reddit: How to terminate or remove the .0 point from an int.
December 9, 2022 -

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)

Discussions

General way to print floats without the .0 part
Iโ€™m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that ... More on discuss.python.org
๐ŸŒ discuss.python.org
19
0
May 19, 2024
How to eliminate trailing zeros?
Since you're already formatting the number as a string, you could just check to see if the last value is a '0', then remove it. Not an efficient solution, but something readable so you have an idea: number = 25.159 num_string = format(number, ".2f") if num_string[-1] == '0': formatted_num = num_string[:-1] else: formatted_num = num_string print(formatted_num) More on reddit.com
๐ŸŒ r/learnpython
13
3
February 12, 2020
python - Inteligently remove ".0" from float output - Stack Overflow
convert 9.0 to 9 but leave 9.5 as it is), you can use float.is_integer to check: More on stackoverflow.com
๐ŸŒ stackoverflow.com
formatting - remove trailing zeros in float representation python - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Using complex functions I can strip zeros and possibly the decimal sign. But it should be a common f-string notation, but I can't find it. ... Okay, thank for the reply. Guess I'll keep the trailing zeros for now. ... my_float = 69.880 result = f'{str(my_float).rstrip("0... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to format floats without trailing zeros?
How to Format Floats Without Trailing Zeros? - AskPython
May 12, 2023 - Python offers four effective methods to remove trailing zeros from float values: the to_integral() and normalize() functions, the str() and rstrip() functions, a while loop, and the float() function.
๐ŸŒ
Sopython
sopython.com โ€บ canon โ€บ 10 โ€บ formatting-a-float-to-remove-superfluous-zeros
Formatting a float to remove superfluous zeros - sopython
3 -> "3" 3. -> "3" 3.0 -> "3" 3.1 -> "3.1" 3.14 -> "3.14" 3.140 -> "3.14" Formatting floats in Python without superfluous zeros
๐ŸŒ
Python Help
pythonhelp.org โ€บ tutorials โ€บ get-rid-of-0
How to get rid of that pesky .0 in Python
The simplest way to remove the โ€œ.0โ€ from a float value in Python is to convert it to an integer using the int() function.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to eliminate trailing zeros?
r/learnpython on Reddit: How to eliminate trailing zeros?
February 12, 2020 -

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

Windows 10 and Python 3.7

๐ŸŒ
Sage Q&A Forum
ask.sagemath.org โ€บ question โ€บ 11051 โ€บ cutting-unnecessary-zeroes-in-float-numbers
Cutting unnecessary zeroes in float numbers - ASKSAGE: Sage Q&A Forum
February 18, 2014 - Then, you can reduce the number of digits by taking x.n(digits=4) (this will also turn an integer into a floating-point number, which you may want to do for large integers). You can then apply the method str with the option no_sci=False to make sure to get a scientific notation regardless of ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ remove-the-trailing-zeros-from-a-decimal-in-python
Remove Trailing Zeros from Decimal in Python
September 3, 2025 - We used str() to convert the variable to a string and then used the .rstrip(0), which stands for the right strip and removes the specified 0 argument from the end of the string. Python provides this simple technique to remove trailing zeros from decimals using the float() function.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-38493.html
method to remove zero digits from fraction in decimal
when a decimal number is a whole (integral, integer-like) number, it gets converted to string without the ".0". but if i take the result of calculation, such as conversion from Fahrenheit to Celsius,
๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ top-10-methods-to-format-floats-without-trailing-zeros
Top 10 Methods to Format Floats Without Trailing Zeros in Python
December 5, 2024 - Effective Number Formatting in Python ยท A: The simplest approach is to use string formatting with rstrip() to remove zeros: ('%.15f' % x).rstrip('0').rstrip('.'). A: Yes, while the Decimal module provides precise control over float representation, it can be significantly slower than other ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-remove-trailing-zeros-from-decimal
How to Remove the trailing Zeros from a Decimal in Python | bobbyhadz
Copied!from decimal import Decimal ... # ๐Ÿ‘‰๏ธ 1.23 ... The first step is to convert the decimal to a string. The str.rstrip() method returns a copy of the string with the provided trailing characters removed....
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74135533 โ€บ remove-trailing-zeros-in-float-representation-python
formatting - remove trailing zeros in float representation python - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Using complex functions I can strip zeros and possibly the decimal sign. But it should be a common f-string notation, but I can't find it. ... Okay, thank for the reply. Guess I'll keep the trailing zeros for now. ... my_float = 69.880 result = f'{str(my_float).rstrip("0").rstrip(".") if "." in str(my_float) else my_float}' print(result)
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74966965 โ€บ how-can-i-remove-the-0-after-floats-in-a-dataframe-without-changing-the-object
python - How can I remove the .0 after floats in a DataFrame without changing the object type? (I have NANs and require numerical sorting) - Stack Overflow
I also wouldn't want "01","02","03"โ€ฆ"10" for the purpose of looks. Becuase this project is really serious, the looks matter, so please don't blame me of overthinking looks of data. ... Would pd.set_option('precision', 0) work for you?
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i get rid of trailing zeros in a float?
How do I get rid of trailing zeros in a float? : r/learnpython
May 11, 2022 - Subreddit for posting questions and asking for general advice about all topics related to learning python. ... Sorry, this post was deleted by the person who originally posted it. Share ... We like the easy answers. Glad this worked for you. ... People have already given you the answer to your question. However... How important is it to round the value really? Most of the time, you will have a real number output (a number with significant decimal places). This means it's stored as a float.