1

I have this code which gives a result

double a = 128.73;
double roundOff = Math.round(a*100)/100;
System.out.println(roundOff);
Result is :- 128.0

what I need that if the values after dot is greater than 5 i.e (6, 7, 8 or 9) then its should give result by adding 1 to the given value that has to be rounded off, i.e

  • 128.54 should give result to 128.0
  • 128.23 should give result to 128.0
  • 128.73 should give result to 129.0
  • 128.93 should give result to 129.0
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
Little bird
  • 1,106
  • 7
  • 28
  • 58
  • 5
    That's an odd way of rounding... – Mysticial Jul 29 '13 at 06:17
  • 1
    @Mysticial : yeah ... thanx for reply ...but i need the same. – Little bird Jul 29 '13 at 06:18
  • 1
    @Mysticial yes, it is very odd, at least that first example has a typo. – Luiggi Mendoza Jul 29 '13 at 06:18
  • 1
    Isn't it done automatically by Math.round this way? Just try Math.round(a) without any multiplication since you don't want decimale places. It should round up/down to next integer. – bkausbk Jul 29 '13 at 06:23
  • @PathFinder: You must specify whether your first example, 128.54 rounding to 128.0, is a mistake or not. People think this is likely a mistake because it is very rare and somewhat unnatural that there is a need to round fractions from 0 to .6 (exclusive) down while rounding .6 (inclusive) to 1 (exclusive) up. If you intended to write that 128.54 should be rounded to 129, not 128, then this is question is a duplicate, as marked. If you truly intend it to be 128, then this is not a duplicate. – Eric Postpischil Jul 29 '13 at 13:44

5 Answers5

2

Why don't you use ROUND_HALF_UP and BigDecimal#setScale?

a = a.setScale(0, BigDecimal.ROUND_HALF_UP);

double myDouble = 55.2; //55.51
BigDecimal test = new BigDecimal(myDouble);
test = test.setScale(0, BigDecimal.ROUND_HALF_UP);
//test is 55 in the first example and 56 in the second

EDIT

As @Alex noticed, the above code will not work as you wish. Here is another way using Math#ceil and Math#floor:

double n = myDouble - Math.floor(myDouble); //This will give you the number 
                                            //after the decimal point.
if(n < 0.6) {
     myDouble = Math.floor(myDouble);
}
else {
     myDouble = Math.ceil(myDouble);
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • It does not work for 128.54 – Alex Jul 29 '13 at 06:44
  • Otherwise it doesn't make a sense. And from my experience in SO, **it is** a typo. If you're not sure, keep your -1 :) – Maroun Jul 29 '13 at 06:51
  • After the proof about typo I will upvote, sorry :) – Alex Jul 29 '13 at 06:52
  • @Alex BTW - "*what I need that if the values after dot is greater than 5 i.e (6, 7, 8 or 9) then its should give result by **adding 1 to the given value that has to be rounded off***". – Maroun Jul 29 '13 at 06:57
  • 1
    So, starting with 128.60 for that numbers (.6,.7,.8 or .9). I understand "value after dot" as 1 (first) digit. – Alex Jul 29 '13 at 07:01
  • @Alex Edited. It should be OK now. – Maroun Jul 29 '13 at 07:06
  • If it was a typo, then this question has a dup that explains solving this using `RoundingMode.HALF_EVEN` (no need to reinvent the wheel using `floor` and `ceil`). – Luiggi Mendoza Jul 29 '13 at 08:00
  • @LuiggiMendoza But it's not.. That's why I used `ceil` and `floor`. – Maroun Jul 29 '13 at 08:03
  • Then looks like homework, and 7 people fall in this trap of OP doing nothing than just posting a question and waiting for us solving the exercise =\ – Luiggi Mendoza Jul 29 '13 at 08:05
  • @LuiggiMendoza You're right. But since he proposed a "solution" (not really..) I though this might help him. – Maroun Jul 29 '13 at 08:11
1

try this, it is working

 double d = 0.51;
 DecimalFormat newFormat = new DecimalFormat("#.");
 double twoDecimal =  Double.valueOf(newFormat.format(d));

"#." = add # after decimal upto what place round off you need .

Jayesh
  • 6,047
  • 13
  • 49
  • 81
1

This works for all your numbers:

BigDecimal.valueOf(128.54).setScale(1, RoundingMode.HALF_UP)
          .setScale(0, RoundingMode.HALF_DOWN)
Maroun
  • 94,125
  • 30
  • 188
  • 241
Alex
  • 11,451
  • 6
  • 37
  • 52
0

If you have really such weired requirements use the folowing code

double value = 128.54;
double rounded = (((value * 10) + 4) / 10)

The value of rounded will then be 128.0. If the value is 128.64 then the result will be 129.0.

If you have normal rounding (.5 and higher rounds up) then you have to change the second line to

double rounded = (((value * 10) + 5) / 10)

The secret is that the constant (4 or 5) must be 10 minus the value from where it should be rounded up.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
-1
  1. Math.round(a*100) makes Math.round(12873) results 12873
  2. result of step 1 12873 is a long typed value but not a double value.
  3. So when it divides by 100, generates an non-decimal result 12873/100 = 128.
  4. Now it stores in a double variable as 128.0.
double roundOff = Math.round(128.54);
System.out.println(roundOff);// output -- 129.0
double roundOff = Math.round(128.23);
System.out.println(roundOff);// output -- 128.0
double roundOff = Math.round(128.73);
System.out.println(roundOff);// output -- 129.0
double roundOff = Math.round(128.93);
System.out.println(roundOff);// output -- 129.0
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125