Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Rounding in Java

Name: Anonymous 2007-10-16 15:07

Whats going on programmers?
I'm currently learning java at a course at my college, today my teacher wanted up to make a program that take FPS and converts it to MPH. I know the math behind it, but he wants it rounded to the 10th decimal. I didn't know how to do this, nor did anyone else, so I just took the output; multiplied by 10; used a rounder for a whole number; then divided by 10. Now while this worked I got it in a roundabout way, so is there a command that would do something like this?
Thanks

Name: Anonymous 2007-10-16 15:22

1. Java is a junk of a language.
2. Afaik, you are lost...

Implement the sucker yourself...

How do you round? I mean not with round(), but with your own math?

...

Boy, I can't find the math for this, any help /prog/?

Name: Anonymous 2007-10-16 15:39

You don't want to round it. Floats/doubles use powers of two, and cannot represent things like 0.1 accurately (it will be 0.100000000000000849 or whatever). Decimal rounding to anything besides integers will not be precise (although if you display the result, the error will usually not be shown).

Instead, you want to format the number:
NumberFormat f = new DecimalFormat("0.0000000000");
System.out.println(f.format(123.456789));

Name: Anonymous 2007-10-16 15:41

just use long integers multiplied by like 10^10

Name: Anonymous 2007-10-16 16:41

>>4
Or use a language which has built-in support for bigints (like forced indentation).

Name: Anonymous 2007-10-17 14:34

var = Math.rint(var*100)/100

2nd decimal... Just play around with the 100's 'till you get what you want.

Name: Anonymous 2007-10-17 15:51

yeah listen to #4 I've also taken java, you need a decimal format.

Name: Anonymous 2009-03-06 11:38


Is NULL and free OR i FAILURE!

Name: Anonymous 2011-01-31 20:11

<-- check em dubz

Name: Anonymous 2011-02-04 15:58


Don't change these.
Name: Email:
Entire Thread Thread List