print(round(mas[i],4))
这仅适用于浮点数,不适用于整数。
Format your number using format()
:
format(mas[i], '.4f')
This formats numbers (including integers) to 4 decimal places. See the Format Specification Mini-Language documentation .
Demo:
>>> print(format(16, '.4f'))
16.0000
>>> print(format(4.242422, '.4f'))
4.2424
If the number is part of a larger string, you can use the format specification in a str.format()
format string , using placeholders:
'Your frobnar has been opfritzed to {:.4f} hafnavz'.format(mas[i])
To be complete, you can also use the printf style formating :
>>> '%0.4f' % 4
'4.0000'
>>> '%0.4f' % 4.56789
'4.5679'
>>> '%0.4f' % .1
'0.1000'
As stated in the documents, it can be quirky with tuples, but can also be faster.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.