Round with Roundup?

How can I round with roundup?

I have done a ton of searching and cam across this bit of code:

private static double round(double a, int b) {

double tempA = a * Math.pow(10, b);

long tempB = (long) tempA;

int diff = (int) (10 * (tempA - tempB));

if (diff >= 5) {

++tempA;

tempB = (long) tempA;

}

tempA = tempB / Math.pow(10, b);

return tempA;

}

It rounds ok but doesn't round up.

For example if the original number is 9.9999994563

and I want to round to 10 decimal places, I would want the number to be

10

Any ideas on how I can do this?

[650 byte] By [stempb] at [2007-9-30 23:13:37]
# 1

> It rounds ok but doesn't round up.

>

> For example if the original number is 9.9999994563

>

> and I want to round to 10 decimal places, I would

> want the number to be

>

> 10

>

> Any ideas on how I can do this?

Well, you aren't using the standard definition of "rounding". That number (9.9999994563) is already rounded to 10 decimal places. So before deciding how to do what you want, we would to know just what it is you want.

DrClap at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...
# 2

Sorry I guess I really mean round to a given percision. I should have been more specific but it was very late and I was not thinking.

Maybe the problem is my orginal formula.

On this call s = 119304640 ( semicircles )

public static double semicirclesToDegrees(int s) {

return (s * (180.0D / Math.pow(2.0D, 31.0D)));

}

The function returns, 9.999999403953552 (sorry should have posted a more accurate number before).

I am reading the 119304640 value from a GPS device. I know it is correct because I have a C++ version of the code that reads the exact number.

The C++ version of the conversion forumla also yields 9.999999403953552.

The number is supposed to be 10.This is the value as entered into the GPS by a user in degrees and then converted to semicircles by the GPS.The value is aslao diaplayed as 10 on the GPS.I have no access to the exact conversion formula used by the GPS.

I have a C++ version of the rounding formula that works. It uses _ecvt to convert the double value 9.999999403953552 to a string with a value of "100000". So it looks like ecvt is rounding up.

I think I'll take a stab at converting my C++ round forumal unless some can see something wrong with my original forumla of.

public static double semicirclesToDegrees(int s) {

return (s * (180.0D / Math.pow(2.0D, 31.0D)));

}

stempb at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...
# 3

Using double calculations is never entirely safe when it comes to rounding. If you need to round to more than 8 digits there are cases where you get rounding errors.

If you want safe rounding use BigDecimal. Don't forget that double is only accurate to a limited number of digits and so the number you get after rounding can be itself an inaccurate value.

private static double round(double a, int b) {

return New BigDecimal(""+a).setScale(b, BigDecimal.ROUND_HALF_UP).doubleValue();

}

Peter-Lawrey at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...
# 4
Thanks very much. But I am still getting a value of 9.999999.
stempb at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...
# 5

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Using double calculations is never entirely safe when it comes to rounding. If you need to round to more than 8 digits there are cases where you get rounding errors.

If you want safe rounding use BigDecimal. Don't forget that double is only accurate to a limited number of digits and so the number you get after rounding can be itself an inaccurate value.

private static double round(double a, int b) {

return New BigDecimal(""+a).setScale(b, BigDecimal.ROUND_HALF_UP).doubleValue();

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Thanks, this actually does work. I am now rounding to 5 decimal places as opposed to 6 . I really only need 5 places anyway.

stempb at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...
# 6

With your formula, each integer value represents a vailue in an interval that is 180?^-31 wide.

180?^-31 = 8.381903171539307E-8

the center of the interval for 119304648 is 10.000 000 074 505 806

the center of the interval for 119304647 is 9.999 999 990 686 774

If the orginal GPS converted 10 degrees to 119304640 rather than 119304647, then what it's doing is not the inverse of your formula.

There is no value you can put into your formula which will be 10.0000000000 rounded to 10dp.

Any result of your formula has an error of ?90?^-31 = 4.190951585769653E-8 = 0.000 000 041 909

You can't expect to get values better than 7 dp out of it, whatever you do.

Pete

pm_kirkham at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...
# 7
Thanks for the confirmation Pete.
stempb at 2007-7-7 13:46:15 > top of Java-index,Security,Event Handling...