The g
formatter limits the output to n
significant digits, dropping trailing zeroes:
>>> "{:.3g}".format(1.234)
'1.23'
>>> "{:.3g}".format(1.2)
'1.2'
>>> "{:.3g}".format(1)
'1'
More Related Contents:
- Python, print all floats to 2 decimal places in output
- How do I get 4 instead of 4.0 in "Squirrels and nuts – 1" task? [closed]
- How do I parse a string to a float or int?
- How can I force division to be floating point? Division keeps rounding down to 0?
- Display number with leading zeros
- How to extract a floating number from a string [duplicate]
- Formatting floats without trailing zeros
- How to check if a float value is a whole number
- Format / Suppress Scientific Notation from Pandas Aggregation Results
- What does %s mean in a Python format string?
- How to truncate float values?
- How to avoid floating point errors? [duplicate]
- How to print float to n decimal places including trailing 0s?
- Python float – str – float weirdness
- Testing floating point equality
- Convert hex to binary
- Convert hex to float
- Format string unused named arguments [duplicate]
- rounding errors in Python floor division
- What is the range of values a float can have in Python?
- What is the difference between int() and floor() in Python 3?
- Print floating point values without leading zero
- How to manage division of huge numbers in Python?
- How do I check if a string represents a number (float or int)?
- Decimal place issues with floats and decimal.Decimal
- Inaccurate Logarithm in Python
- Python 3 bytes formatting
- Function to determine if two numbers are nearly equal when rounded to n significant decimal digits
- Number of digits in exponent
- Rounding floats so that they sum to precisely 1