Rounding algorithm for Java

Source: Internet
Author: User

Rounding of Coarse force is an integer

1  PackageMath; 2   3  Public classMathroundtest {4     /** 5 * Three methods related to rounding are provided in the Math class: Ceil,floor,round,6 * These methods correspond to the meanings of their English names, for example: the English meaning of Ceil is the ceiling, which means rounding up,7 * Math.ceil (11.3) Results for 12,math.ceil (-11.6) results for -11;floor English is the floor,8 * The method represents a downward rounding, and the result of Math.floor (11.6) is the result of 11,math.floor (-11.4) -12;9 * The most difficult to master is the round method, he said, "rounding", the algorithm for Math.floor (x+0.5), the original number plus 0.5 and then rounded down,Ten * Therefore, the result of Math.Round (11.5) is that the result of 12,math.round (-11.5) is-11. Math.Round () conforms to this rule: One * After the decimal point is greater than 5 all plus, equal to 5 positive plus, less than 5 all without adding.  A      */   -      Public Static voidMain (string[] args) { -System.out.println ("The first =5 after the decimal place");  theSystem.out.println ("Positive: math.round (11.5) =" + Math.Round (11.5));  -SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.5) =" + Math.Round (-11.5));  - System.out.println ();  -      +System.out.println ("The first <5 after the decimal place");  -System.out.println ("Positive: Math.Round (11.46) =" + Math.Round (11.46));  +SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.46) =" + Math.Round (-11.46));  A System.out.println ();  at      -System.out.println ("The first >5 after the decimal place");  -System.out.println ("Positive: Math.Round (11.68) =" + Math.Round (11.68));  -SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.68) =" + Math.Round (-11.68));  -         /**  - * Operation Result: in 1, the first place after the decimal =5 - 2. Positive number: Math.Round (11.5) =12 to 3, Negative number: Math.Round ( -11.5) =-11 + 4. - 5, the first place after the decimal <5 the 6. Positive number: Math.Round (11.46) =11 * 7, Negative number: Math.Round ( -11.46) =-11 $ 8.Panax Notoginseng 9, the first place after the decimal >5 - 10. Positive number: Math.Round (11.68) =12 the 11, Negative number: Math.Round ( -11.68) =-12 +          */   A         /**  the * 1, the first digit after the decimal point <5, the result of the operation is the integer part of the parameter.  + 2, the parameter of the first digit after the >5, the result of the parameter integer part of the absolute value +1, the symbol (that is, plus or minus) unchanged.  - 3, the parameters of the first digit after the decimal = 5, the positive result is the integer part +1, the result of the negative operation is an integer part.  $          */   $     }      -    -}
View Code

Rounding algorithm for Java

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.