4

I dont get how rounding numbers up to certain decimal places I looked everywhere tried every thing

currently I have my program to round up to a whole number with double rACT = Math.ceil(ACT); double rSAT = Math.ceil(SAT); double rGPA = Math.ceil(GPA);

but i need it to round up to 2 decimal places

FYI - I am an High school student I really dont need something super complicated to do this cuz I need my methods to be less then 15 I can waste any lines

Tom
  • 43,583
  • 4
  • 41
  • 61
Heon Jun Park
  • 49
  • 1
  • 1
  • 2
  • 1
    http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Vishal Oct 28 '11 at 04:19
  • There is difference between rounding a number (to N-places) and obtaining a *string representation* with rounding/truncation rules applied... –  Oct 28 '11 at 04:27

4 Answers4

11

There's probably a simpler way, but the obvious is:

double rSAT = Math.ceil(SAT * 100) / 100;

This turns a number like 2.123 into 212.3, rounds it to 213, then divides it back to 2.13.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • Actually, a call to `Math.ceil()` will round the number `212.3` to `213`. In math the ceiling function will round any number greater than a whole number to the next available whole number, so even `212.0003` would be rounded to `213`. You could use `Math.round()` which performs a standard rounding function and is equivalent to `Math.floor(decimal + 0.5)`. – Brandon Buck Oct 28 '11 at 17:16
  • @izuriel - The question specifies rounding numbers **up** (and they use `Math.ceil` too). Fixed my explanation though to say that it rounds 212.3 to 213. – Brendan Long Oct 28 '11 at 17:31
  • My take was that he was asking how to round a number and was giving the solution he was able to come up with. Either way, you specifically state that calling `Math.ceil()` on `212.3` will yield `212` which is incorrect, which was the main focus of my comment to inform you of that error. – Brandon Buck Oct 28 '11 at 17:34
2

Usually, rounding is best done at the point of rendering the number (as a String, e.g.). That way the number can be stored/passed around with the highest precision and the information will only be truncated when displaying it to a user.

This code rounds to two decimal places at most and uses ceiling.

double unrounded = 3.21235;
NumberFormat fmt = NumberFormat.getNumberInstance();
fmt.setMaximumFractionDigits(2);
fmt.setRoundingMode(RoundingMode.CEILING);

String value = fmt.format(unrounded);
System.out.println(value);
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
0

The question has been asked before, check out How to round a number to n decimal places in Java

the simplest solution, in my opinion, is this one, by chris:

double myNum = .912385;
int precision = 10000; //keep 4 digits
myNum= Math.floor(myNum * precision +.5)/precision;
Community
  • 1
  • 1
aleph_null
  • 5,766
  • 2
  • 24
  • 39
0

If you are looking for a string representation of a number you can do like below:

DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(12.912385));
Vishal
  • 19,879
  • 23
  • 80
  • 93