Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Rounding method in python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "rounding method in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Traditional round function

In python, rounding is not as simple as imagined in numerical operation. When the program is running, it is actually converted to binary for operation. The possible errors in the conversion process are due to the fact that the precision of floating-point numbers may not be accurately expressed in the process of binary conversion, which leads to the loss of precision. Therefore, in python, if there is a certain requirement for precision, do not use the round function, but recommend using the decimal library.

For example:

> > xylene 123.455

> > x

123.455

> > a=round (x)

> > a

one hundred and twenty three

> a=round (xprimel)

> > a

123.5

> a=round (xprime2)

> > a

123.45 # this result has an error

From this case, we can see that when we use the round function for rounding, we need to pay attention to the correctness of the results, but there will be accuracy errors.

2. Decimal with required precision

Python used to calculate the decimal module Decimal module, you can view the loss of binary progress.

> > x

123.455

> print (decimal.Decimal (x))

123.4549999999999982946974341757595539093017578125

Question: how to solve the rounding problem caused by the loss of binary precision storage?

First, import

From decimal import Decimal, Context, ROUND_HALF_UP

Then, start the four-shot and five-entry method.

> a=decimal.Context (prec=5,rounding=decimal.ROUND_HALF_UP). Create_decimal ('123.455')

> > a

Decimal ('123.46')

Pay attention to the wrong way and the right way

Xerox 123.455

In the wrong way, the parameter is not converted to a string

> a=decimal.Context (prec=5,rounding=decimal.ROUND_HALF_UP) .create_decimal (x)

> > a

Decimal ('123.45')

In the correct way, you need to convert x to a string first.

> a=decimal.Context (prec=5,rounding=decimal.ROUND_HALF_UP). Create_decimal (str (x))

> > a

Decimal ('123.46')

Explanation: round off the functions you need to use

The Context function, which has many parameters, has default values. When we do rounding, we only need to change the values of two parameters to set the context processing environment of python.

Parameter 1 is the prec parameter, which is used to display the final number of digits, which contains integer and decimal parts.

Parameter 2 is the rounding parameter, changed to ROUND_HALF_UP as a round.

Create_decimal function. Note that the parameter value is a string.

III. Summary and emphasis

1. By default, the python environment uses the banker's algorithm / thinking.

two。 Flexible use of rounded several functions context function, create_decimal function.

3. Master the way to check the precision error of binary storage by using the Decimal function under the decimal module.

This is the end of "rounding methods in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report