how to round off a float [duplicate] - python

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

Related

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.

Force python to print a certain number of decimal places [duplicate]

This question already has answers here:
Python Decimals format
(6 answers)
Closed 3 years ago.
I have a python program which takes some floating type values and writes to a file.
I round these numbers to 6 decimal places and then convert to a string type before writing to file.
file.write(str(round(value,6)))
However with certain numbers the value written to file is in the format shown below.
e.g. 3e-06 or 4e-03
How can I avoid this and instead write out in decimal format like
0.000003 and 0.004000
How can I print exactly 6 figures after the decimal point.
You can use the f-string f'{value:.6f}'.
Example:
value = 0.234
print(f'{value:.6f}')
value = 1
print(f'{value:.6f}')
value = 0.95269175
print(f'{value:.6f}')
Output:
0.234000
1.000000
0.952692
Also, in the answer linked in a comment, there was reference to :g. That can work, but probably not in this situation, because g may print scientific notation where appropriate, and discards insignificant zeroes. Consider a slightly modified example using g:
value = 0.234
print(f'{value:.6g}')
value = 1
print(f'{value:.6g}')
value = 0.000000000095269175
print(f'{value:.6g}')
Output:
0.234
1
9.52692e-11
You can also use basic string formatting:
a = 3e-06
# Outputs 0.000003
print('%.6f' % a)
# Outputs 0.000003000000
print('%.12f' % a)

Python convert int to string preserving zeros [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 6 years ago.
i'd like to convert a decimal to a string, where zeros at the end are preserved.
Using str method erases the last zeros.
Example:
number=0.20
Goal: "0.20"
e.g. using: str(number)="0.2" doesn't seem to work.
If you want 2 decimal places use:
number = 0.20
str_number = '%.2f' % number
Number before f indicates the desired number of places.
This can be done using string formatting.
"{0:.2f}".format(number)
Will return 0.20.
Doing your chosen method won't work because upon declaring number = 0.20 it omits the last zero right away. If you put that into your idle:
number = 0.20
number
0.2
So declaring number as str(number) is doing str(0.2).
Use the % operator with an appropriate format string:
'%1.2f' % number
=> '0.20'

Converting a number in exponential form to decimal form in python [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 8 years ago.
I have a very silly question, suppose if i have a number 1.70000043572e-05 how should I convert it into float i.e. 0.0000170000043572.
You need to convert to a float and use str.format specifying the precision:
In [41]: print "{:f}".format(float("1.70000043572e-05"))
0.000017
# 16 digits
In [38]: print "{:.16f}".format(float("1.70000043572e-05"))
0.0000170000043572
Just calling float would give 1.70000043572e-05.
Using older style formatting:
In [45]: print( "%.16f" % float("1.70000043572e-05"))
0.0000170000043572
If you are just inputting the number into your script python will understand that as a float.
If it is a string then use the builtin float on it to do the conversion for example:
x = float("0.423423e4")
print "%.2f" % (x)
will output
4234.23

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