SoFunction
Updated on 2024-11-18

Python retains a number of decimals Difference between the use of format and round

Python retains a number of decimal places format vs round

We often use the format and round functions when working with data.

Both are capable of retaining a number of decimal places, but the process is slightly different.

difference

  • The return type is different: the format function is formatted to return the result of the data type is str strings, round function returns the result of the data type is float floating-point type
  • Rounding is different: when retaining a number of decimal places, format follows the principle of rounding, while round does not follow the principle, round round the principle of rounding to the next even.
  • Bit retention is different: provisions such as retaining 6 decimal, format function output results after the decimal point is to retain 6 decimal, and round function is slightly more flexible, it will be behind the 0 end of the decimal are removed, that is, to retain the non-zero part.
  • Functions are different: round function mainly used in floating-point data used to retain a number of decimals, format function can not only retain a number of decimals, its main function is to format strings, customized output strings.

round function

the principle of precedence (in calculus)

what is a four-square measure?subdivision of the universe consisting of six elementsEven into? Look at the last digit, if the last digit is less than or equal to 4, then round off, if the last digit is greater than or equal to 6, then enter one; if the last digit is 5, then we have to discuss the situation: if the penultimate digit is an even number, then enter one, and if it is an odd number, then round off.

typical example

>>> round(2.674,2)  # If the parameter is 2, keep two decimal places, look at the third decimal place, and round off if it is less than or equal to 4
2.67
>>> round(2.645,2)  # If it's equal to 5, look at the penultimate digit. 4 is an even number, so if it's even, it's one digit.
2.65
>>> round(2.655,2)  # 5 is odd, next even, so round up
2.65
>>> round(2.675,2)
2.67
>>> round(2.685,2)
2.69
>>> round(2.695,2)
2.69
>>> round(2.676,2)  # The third decimal is greater than or equal to 6, so go one place.
2.68
>>> type(round(2.674,2))  # The result is floating point
<class 'float'>
>>> round(3.677,6)  # When the required number of retained decimals exceeds the actual number of decimal places, only the non-zero portion is retained
3.677

format function

format function is powerful, written in a variety of formats, we combined with examples to illustrate the arithmetic process.

typical example

>>> format('2.674','.2f')   # When using this method to preserve decimals, the first parameter must not be of type string
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    format('2.674','.2f')
ValueError: Unknown format code 'f' for object of type 'str'
>>> format(2.674,'.2f')  # Correct format '.2f' means retain two decimal places
'2.67'
>>> format(3.677,'.6f')  # Decimals will be retained to the extent that they are required to be retained
'3.677000'
>>> print("lishuaige is %.3f ah." % 6.66555)  # Fixed-point digitally formatted output
lishuaigejust like6.666interjection of surprise
>>> print("lishuaigejust like{:.3f}interjection of surprise".format(6.66555))
lishuaigejust like6.666interjection of surprise

Python's common output retains several decimals

Retain two decimal places (rounded)

a = 1.086
# Method I
print("%.2f" %a)
# Method II
print("{:.2f}".format(a))
# Method III
a1 = round(a,2)
print(a1)

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.