how to format float number in python? [duplicate] - python

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 4 years ago.
I want to format my float number with 2 digit after decimal.
>>> x =5.0
>>> y=float("{:0.2f}".format(x))
>>> y
5.0
i want my output in this format:
5.00

For newer version of python you can use:
x = 5.0
print(f' x: {x:.2f}')
out put will be:
x: 5.00
for more about this style see: f-string

You can do it by
In [11]: x = 5
In [12]: print("%.2f" % x)
5.00
In [13]:

Your answer was correct. you just misplaced the colon:
print "{:.2f}".format(5.0)
#output:
'5.00'
;)

Related

how to round off a float [duplicate]

This question already has answers here:
How can I format a decimal to always show 2 decimal places?
(13 answers)
Closed 3 months ago.
i want to round off a float to 3 dp in python with 00 in the end if the float don't have 3 dp
like 15.4 into 15.400
thank you.
programme:
x=round(15.4)
result:
15.400
The "rounding" you are talking about can only be done if you convert the float to a string. This is usually only done for display purposes. In this case you can use a so-called f-string to do this formatting:
x = 15.4
print(f"{x:.3f}")
Hello its pretty simple you can do something like this
a=15.4
b=("%.3f" % a)
print(b)
15.4 and 15.400 are the same number. round() returns a number. What you want is to have a different representation when you convert it to a string.
You need to do string formatting. Just copying the other answers here, there are two ways.
f-strings:
n = 15.4
n_str = f"{n:.3f}"
%-formatting:
n_str = "%.3f" % n

Can someone explain this behavior with Python? [duplicate]

This question already has an answer here:
Python int float rounding
(1 answer)
Closed 3 years ago.
Can someone explain this behavior?
print("%.2f" % (model.x[1].value))
X = int(model.x[1].value)
print("%.2f" % X)
Output:
3.00
2.00
Thanks
When model.x[1].value is less than 3 but >= 2.995 then when printed to 2 decimal places it will round up to 3.00. But when you apply the int function it will be truncated to an integer of value 2, which will print as 2.00 when used with the .2f format.
>>> x = 2.994
>>> print(f"{x:.2f}")
2.99
>> x = 2.995
>>> print(f"{x:.2f}")
3.00
Thanks guys. The value is indeed not an integer. I was fooled by a mixed integer nonlinear programming solver. It seems that it doesn't round the solution to integer by itself.

How do I print double in Python with exact precision? [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 7 years ago.
I need to print double with precision equal exactly to 6, I found function round:
print(str(round(result, 6))
But in case result itself has less precision, the print function skips zeros at the end.
Gor example, the output of such code,
print(str(round(4.0, 6)))
is
4.0
But what I need is
4.000000
How can I reach this?
Try using a format string:
print("%.6f"%4.0) # 4.000000
Or alternatively:
print("{:.6f}".format(4.0))
See the Python documentation for details on format strings and more examples.

How to truncate numbers? [duplicate]

This question already has answers here:
Python 3 Float Decimal Points/Precision [duplicate]
(5 answers)
Closed 7 years ago.
I have digits, for example:
100.1264
9.09
123
1298.456789
and I want to truncate this to:
100.12
9.09
123
1298.45
How to truncate those numbers so as to leave their form?
If you have numbers (not strings), you can use:
import math
math.trunc(x * 100) / 100
For example:
>>> import math
>>> x = 100.1264
>>> math.trunc(x * 100) / 100
100.12
You may also use int in place of math.trunc, however beware that casts between floats and ints may be computationally expensive.
Bonus tip: if you want arbitrary precision decimal arithmetic, take a look at the decimal module.
s="""100.1264
9.09
123
343.1
1298.456789"""
print re.sub(r"(?<=\.)(\d{2})\d+",r"\1",s)
If your input is a float you can use
s=100.1264
print ast.literal_eval(re.sub(r"(?<=\.)(\d{2})\d+",r"\1",str(s)))

python format a floating number to print .123 not 0.123 [duplicate]

This question already has answers here:
Print floating point values without leading zero
(13 answers)
Closed 8 years ago.
I have a simple question, that I feel should have a simple solution. How do I format a floating number so that only the numbers after the decimal point show? I would prefer to use '{}'.format to accomplish this.
>>> n = 0.12345
>>> n
0.12345
>>> str(n)[1:]
'.12345'
>>> '{}'.format(n)
'0.12345'
>>> '{}'.format(str(n)[1:])
'.12345'
I know I can use str(n)[1:], but I'd prefer not to have to convert the number to a string.
I do not think there is a format string which removes the zero. However, you could use lstrip:
In [25]: n = 0.12345
In [26]: '{:.3f}'.format(n).lstrip('0')
Out[26]: '.123'
At least that is safer than str(n)[1:], which would remove a signficiant digit if n were equal to a number bigger than 1 or less than -1.

Categories

Resources