. Net round Truncate Ceiling function

Source: Internet
Author: User
Tags rounds

Round: rounds the point value to the nearest integer to convert the specified pointf to a point object.
See the following example:

Math. round (3.44, 1); // returns 3.4. 4

Math. round (3.451, 1); // after returns 3.5, a non-zero entry
Math. round (3.45, 1); // returns 3.4. After, check for parity.

Math. round (3.75, 1); // after returns 3.8, check for parity. Before five, it is odd to enter one.
Math. round (3.46, 1); // returns 3.5. Six entries

 

If we want to implement our traditional rounding function, a relatively simple method is to add 0.0000000001 to the back of the number, a small number. because "after five is not zero, it can be ensured that 5 must enter one.

Of course, you can also write functions by yourself. The following code is provided:

Public static decimal unit = 0.0.1m

Static public decimal round (decimal d)

{

Return round (d, unit)

}

 

Static public decimal round (decimal d, decimal unit)

{

Decimal rm = d % unit;

Decimal result = d-rm;

If (rm> = unit/2)

  {

Result + = unit;

  } 

Return result;

}

Note that round is much more powerful than it seems to be simply because it can fully be a specific number of decimal places. All other rounds are always zero decimal. For example:

N = 3.145;
A = system. math. round (n, 2, midpointrounding. toeven); // 3.14
B = system. math. round (n, 2, midpointrounding. awayfromzero); // 3.15

 

 

Truncate: essentially removes the decimal part and moves closer to 0. For example, the coordinates of 0.9 and-0.9 are changed to 0.

Ceiling: move closer to the next largest integer. For example, if 0.9 is changed to 1,-0.9 is changed to 0.

With other functions, you must use multiplication/Division fraud to achieve the same effect:

C = system. math. truncate (n * 100)/100; // 3.14
D = system. math. ceiling (n * 100)/100; // 3.15

 

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.