Java Division Two ways to keep the two digits after the decimal point summary of Floor,round and ceil of Java Math

Source: Internet
Author: User

Floor returns the largest integer not greater than
Round is the calculation of 4 5 in, when it is greater than its integer
Round method, which means "rounding", the algorithm is Math.floor (x+0.5), the original number is added 0.5 and then rounded down, so the result of Math.Round (11.5) is 12,math.round (-11.5) The result is-11.


Ceil is not less than his smallest integer


See Example

Math.floor Math.Round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1


The test procedure is as follows:

[Java]View Plaincopyprint?
  1. Public class MyTest {
  2. public static void Main (string[] args) {
  3. double[] Nums = { 1.4, 1.5, 1.6,-1.4,-1.5,-1.6};
  4. For (double num:nums) {
  5. Test (NUM);
  6. }
  7. }
  8. private static void Test (double num) {
  9. System.out.println ("math.floor (" + num + ") =" + Math.floor (num));
  10. System.out.println ("math.round (" + num + ") =" + Math.Round (num));
  11. System.out.println ("math.ceil (" + num + ") =" + Math.ceil (num));
  12. }
  13. }



Run results
Math.floor (1.4) =1.0
Math.Round (1.4) =1
Math.ceil (1.4) =2.0
Math.floor (1.5) =1.0
Math.Round (1.5) =2
Math.ceil (1.5) =2.0
Math.floor (1.6) =1.0
Math.Round (1.6) =2
Math.ceil (1.6) =2.0
Math.floor (-1.4) =-2.0
Math.Round (-1.4) =-1
Math.ceil (-1.4) =-1.0
Math.floor (-1.5) =-2.0
Math.Round (-1.5) =-1
Math.ceil (-1.5) =-1.0
Math.floor (-1.6) =-2.0
Math.Round (-1.6) =-2
Math.ceil (-1.6) =-1.0

http://blog.csdn.net/foart/article/details/4295645

1. Using the Math.Round () method:

Divides the number of two int, and the result retains two digits after the decimal point:

int a=1188;
int b=93;
Double C;

C= (Double) (Math.Round (A/b)/100.0);//This keeps 2 bits
Print Result: c=0.12

C=new Double (Math.Round (A/b)/1000.0);//This keeps 3 bits
Print Result: c=0.012


2. Another approach
Import Java.text.DecimalFormat;

DecimalFormat DF2 = new DecimalFormat ("###.00");//This is to keep 2 bits

DecimalFormat DF2 = new DecimalFormat ("###.000");//This is to keep 3 bits

SYSTEM.OUT.PRINTLN (Df2.format (variable of type double));

Ps:

The role of Math.Round ():

Double a=123.55
System.out.println (Math.Round (a));
Printed results: 124

http://blog.csdn.net/evatian/article/details/4398016

Java Division Two ways to keep the two digits after the decimal point summary of Floor,round and ceil of Java Math

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.