In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "python how to control output decimal accuracy". In daily operation, I believe many people have doubts about how python controls output decimal accuracy. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to control output decimal precision by python". Next, please follow the editor to study!
First, less precision is required.
Convert high-precision floating-point numbers into low-precision floating-point numbers.
1.round () built-in method
Round () is not a simple rounding method.
> round (2. 5) 2 > round (1. 5) 2 > round (2.675) 3 > round (2.675, 2) 2.67
Round () if there is only one number as an argument, when the number of digits is not specified, it returns an integer and the nearest integer (which is similar to rounding). But when .5 occurs, the distance on both sides is the same, and round () takes the even number 2 that approaches.
When specifying the number of decimal places to choose, the rule of rounding is also used, but in the case of .5, if the tree before the number of decimal places to be chosen is odd, it will be discarded directly, if the even number is an upward trade-off.
Look at the following example:
> > round (2.635, 2) 2.63 > round (2.645, 2) 2.65 > round (2.655, 2) 2.65 > round (2.665, 2) 2.67 > round (2.675, 2) 2.672. Use formatting
The effect is the same as round ().
> a = ("% .2f"% 2.635) > axi2.63'> a = ("% .2f"% 2.645) > axi2.65'> a = int (2.635) > a2 2, precision analysis requiring more than 17 digits
Python defaults to 17 decimal places, but there is a problem here, that is, what should we do when we need to use higher precision (more than 17 decimal places) in our calculations?
1. Use formatting (not recommended) > > a = "% .30f"% (1max 3) > > axiom 0.33333333333333333314829616256247'
It can be displayed, but it is not accurate, and the following numbers are often meaningless.
two。 Use decimal module with getcontext > from decimal import * > print (getcontext ()) Context (prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,capitals=1, clamp=0, flags= [], traps= [InvalidOperation, DivisionByZero,Overflow]) > > getcontext (). Prec= 50 > b = Decimal (1) / Decimal (3) > > bDecimal ('0.333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
The default context precision is 28 bits, which can be set to 50 bits or more. In this way, we can have a higher precision that we can control when analyzing complex floating-point numbers.
In fact, you can pay attention to the rounding=ROUND_HALF_EVEN parameter ROUND_HALF_EVEN in context. When half, it is close to even.
3. About decimals and rounding
Since we talk about decimals, we must talk about integers. These functions are generally used for rounding:
1. Round ()
Forget about this. I've already talked about it before. It is important to note that it is not a simple rounding, but a ROUND_HALF_EVEN strategy.
2. Ceil (x) of math module
Take the smallest integer greater than or equal to x.
3. Floor (x) of math module
To be the largest integer less than or equal to x.
> from math import ceil, floor > round (2.5,3 > floor (2.52) 2 > round (2.32) > ceil (2.33) 3 > floor (2.32) 2 > at this point, the study on "how python controls the output decimal accuracy" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.