Python algorithm for rounding up

Source: Internet
Author: User
Tags floor division

First, the original intention:

Sometimes we need to calculate the number of pages when we display the data on the page. is generally rounded up, such as counts=205 pagecouts=20, pages= 11 pages.

The general division is only the integer part, which does not reach the requirement.

Second, the method:

1. General Division:

Up (A/b) = Int ((a+b-1)/b)

Take the critical value, calculate the range of the a+b-1 is OK.

2. Python Division:

The first thing to say is the division in Python, where there are two divisions in Python version 2.5, the so-called true division and floor division.

when using the X/Y form for division operations, if Y and X. Are all shaped, then the operation will intercept the result, taking the integer part of the operation, such as 2/3, the result of the operation is 0; if one of the X and Y is a floating-point number, then the so-called true division, such as the result of 2.0/3, is 0.66666666666666663.

Another division is in the form of x//y (rounding down), then the so-called floor division, that is, the maximum integer value is not greater than the result, this operation is independent of the operand. For example, the result of 2//3 is that the result of 0,-2//3 is -1,-2.0//3-1.0.

In Python 3.0, X/y will only perform a true division, regardless of the operand; x//y performs floor division.

If you need to use this in the 2.5 version of Python, you will need to include the declaration of the From __FUTURE__ Import division before the code. Such as:

 from __future__ Import Division  a=2/3                  

At this point, the result of variable a will be 0.66666666666666663 instead of the original 0.

Python arithmetic Up Rounding Method: (a+b-1)/b

3. Python Match.ceil function

The ceil (x) function is rounded up, taking the nearest integer greater than or equal to X.

Import Math

Math.ceil (Float (205)/20)

In addition: Rounding down, generally using floor Division, General Division/,round () round function.

Python algorithm for rounding up

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.