In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is a detailed introduction to "what is the difference between format function and round function in python". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "what is the difference between format function and round function in python" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.
the difference
Different return types: format function returns str string after formatting, round function returns float
Different carry: When several decimal places are reserved, format follows the rounding principle, while round does not follow this principle. The round carry principle is rounded and even.
The number of digits reserved is different: if the 6 decimal places are reserved, the format function outputs the result with 6 decimal places reserved after the decimal point, while the round function is slightly flexible, and it will remove the decimal places ending with 0, that is, retain the non-zero part.
Functions are different: round function mainly acts on floating-point data to retain a number of decimals, format function can not only retain a number of decimals, its main function is to format strings, custom output strings.
round function carry principle
What is rounding sub-coupling? Look at the last digit, if the last digit is less than or equal to 4, then drop it, if the last digit is greater than or equal to 6, then enter one digit; if the last digit is 5, it is necessary to discuss the situation: if the penultimate digit is even, enter one digit, and if it is odd, drop it.
Example>>> round (2.674,2) #If the parameter is 2, keep two decimal places, look at the third decimal place, if it is less than or equal to 4, then drop 2.67>> round (2.645,2) #= 5, then look at the penultimate digit, 4 is even, sub-even, then enter one digit 2.65>> round (2.655,2)#5 is odd, sub-even, so drop 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 place is greater than or equal to 6, so enter one digit 2.68>> type(round(2.674,2)) #The result is floating point type>>> round(3.677,6) #When the specified decimal places exceed the actual number of decimal places, only the non-zero part is retained 3.677format function
Format function is powerful, writing format is various, we combine examples to explain the operation process.
Example>>> format ('2.674','.2f') #When using this method to reserve decimals, the first argument must not be a string type Traceback (most recent call last): File "", line 1, in format ('2.674','.2f') ValueError: Unknown format code 'f' for object of type 'str'>> format ('2.674','.2f')#Correct format '.2f' means to retain two decimal places '2.67'>>> format (3.677,'.6f')#Specify how many decimal places to keep will keep a few decimal places'3.677000'>>> print ("lishuaige is %.3f ah" % 6.66555) #fixed-point digital format output lishuaige is 6.666 ah>> print ("lishuaige is {:.3f} ah".format(6.66555))lishuaige is 6.666 ah read here, this article "python format function and round function what is the difference" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.