Python retains decimals - '%f'
'%.nf' % x (defined variable)
example:
a = 82.16332
print('%.1f'% a)
print('%.2f'% a)
print('%.3f'% a)
print('%.4f'% a)
print('%.10f'% a)
output result
Python retains decimals - format () function
Starting from Python2.6, a new format string function str.format() has been added , which enhances the string formatting function.
The basic syntax is to replace the previous % with {} and : .
The format function can accept unlimited parameters, and the positions can be out of order.
example
print("{:.1f}".format(0.167123))
print("{:.2f}".format(0.167123))
print("{:.3f}".format(0.167123))
print("{:.4f}".format(0.167123))
print("{:.10f}".format(0.167123))
output result
Because the position of the format() function can be out of order, it can also be written like this
print(format(0.167123, '.1f'))
print(format(0.167123, '.2f'))
print(format(0.167123, '.3f'))
print(format(0.167123, '.10f'))
output result
By observing the data, it can be found that the above two data are 'rounded'.
Python retains decimals - round() function
The round() method returns the rounded value of the floating point number x.
Usage: round (number, n), n is "numerical expression, indicating the number of digits from the decimal point".
Example
s = 1.15457321
print("round(80.23456, 2) : ", round(80.23456, 2))
print( round(1203245, 3))
print(round(s,10))
output result
And for the round() function sometimes there are some problems, sometimes it doesn't give correct output
example
print(round(2.675,2))
print(round(8.875,2))
output result
If you follow the 'rounding', it should output 2.68 , but the result is 2.67, and the output of 8.875 is the 'rounding' value 8.88
For the python round() function
The round function in python rounds the decimal value to the given number of digits, if we don't provide n (that is the number of digits after decimal), the number is rounded to the nearest integer.
example
#int
print(round(12))
#float
print(round(66.6))
print(round(45.5))
print(round(92.4))
output result
When the second parameter is provided, if the provided parameter n>=5, the last decimal digit will be increased by one until the rounded value, otherwise it will be the same as the provided one
example
print(round(1.221, 2))
print(round(1.222, 2))
print(round(1.223, 2))
print(round(1.224, 2))
print(round(1.215, 2))
print(round(1.226, 2))
print(round(1.227, 2))
print(round(1.228, 2))
print(round(1.279, 2))
output result
It can be seen that some carries are successful, and some are unsuccessful, so this round() function is not easy to use, but the carry of integers is correct
So it is not recommended to use this method
Python retains decimals - math.floor
floor() Returns the rounded integer of a number.
To use the floor method, you need to introduce the math module
floor()表示的是向下取舍
例子
import math
print("math.floor(-45.17) : ", math.floor(-45.17))
print("math.floor(100.12) : ", math.floor(100.12))
print("math.floor(100.72) : ", math.floor(100.72))
print("math.floor(119L) : ", math.floor(119))
print("math.floor(math.pi) : ", math.floor(math.pi))
输出结果
进行小数操作可以这样使用,先进行扩大数值的n次方,然后再除以n次方,即可得到’四舍五不入‘的数值
例子:(保留两位小数)
import math
print(math.floor(1.25754*10**2)/10**2)
输出结果
如果想要输出三位小数不进行‘四舍五入’,可以先乘10**3然后除以10**3
python保留小数——不进行四舍五入,简单粗暴法int()
例子
print(int(1.23456 * 1000) / 1000 )
输出结果
放大指定的倍数,然后取整,然后再除以指定的倍数。
可以利用上面的函数进行保留2位小数、3位小数、4位小数等