Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Round to digits
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
KillCloud




PostPosted: Wed Oct 11, 2006 6:03 pm   Post subject: Round to digits

Please tell me how to round a number to digits, like round 1.345666 to 2 digits, thus, 1.35.
Sponsor
Sponsor
Sponsor
sponsor
KillCloud




PostPosted: Wed Oct 11, 2006 6:14 pm   Post subject: (No subject)

Ok i know that
code:
System.out.printf("%.2f", someNumber);

will round to 2 decimal place but i dont understand how it works
can someone explain it? thx
Andy




PostPosted: Tue Oct 17, 2006 11:39 pm   Post subject: (No subject)

why not look it up on the [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)]java[/url] api?
ericfourfour




PostPosted: Thu Oct 19, 2006 10:44 pm   Post subject: (No subject)

Or you can make your own method. Here;s how it could look.

code:

int mult_by = 10 ^ decimal_places
int rounded = (int) (num * mult_by + 0.5)
double new_num = rounded / (mult_by * 1.0)


I'm pretty sure that should work.
zylum




PostPosted: Fri Oct 20, 2006 12:00 am   Post subject: (No subject)

you could do a little bit better:

code:
double mult_by = Math.pow(10, decimal_places);
double rounded = Math.round (num * mult_by);
double new_num = rounded /mult_by;


*note this is untested but you get the point.. ei use the round() method rather than adding 0.5 and casting. also, if you are dealing with 2 ints and you are dividing and you want to keep decimal places, cast one of the ints to a double rather than multiply by 1.0
ericfourfour




PostPosted: Fri Oct 20, 2006 4:31 pm   Post subject: (No subject)

another improvement would be:
code:

double mult_by = 1edecimal_places

That one can use negatives to you could round to negative decimals.
For example:
num = round (127, -2)
num would equal 100 (or 130 I can't think right now).
ericfourfour




PostPosted: Sun Oct 22, 2006 2:35 am   Post subject: (No subject)

This one should work. I added 0.5 and casted to an integer because that way it isn't dependant on Math.round (using a separate rounding method for your rounding method doesn't make much sense). I also threw in a power method because the one in the Math class doesn't do negatives. This one has been tested and it works.

code:
public static double power (double base, int exponent)
{
    if (exponent < 0) return power (base, exponent + 1) / base;
    else if (exponent > 0) return power (base, exponent - 1) * base;
    return 1;
}
   
public static double round (double num, int decimal_places)
{
    double mult_by = power (10, decimal_places);
    double rounded = (int) (num * mult_by + 0.5);
    return rounded / mult_by;
}


code:
System.out.println (round (1251.345, -1));
Output: 1250.0

System.out.println (round (1251.345, 2));
Output: 1251.35

System.out.println (round (1251.345, 0));
Output: 1251.0
Ultrahex




PostPosted: Sun Oct 22, 2006 10:15 pm   Post subject: (No subject)

Im Hoping You guys know that there is something called DECIMAL FORMATTING in java...... oh well lol
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Mon Oct 23, 2006 9:15 am   Post subject: (No subject)

Guys, don't reinvent the wheel:

Math.round() will return a long type rounded to the nearest 1.
Math.pow(double x, double y) will return x^y

To round to 2 decimal places...

Java:
int x = 2.6666666666;
x = (int) (Math.round(x * 100) / 100);


Or, a simple function:

Java:
public long roundTo(double x, int places) {
  return Math.round(x * Math.pow(10, places)) / Math.pow(10, places'
}


That way, you can round to the nearest hundred (so 155 will be 200) by specifying -3 as the 'places'. Or something like that. No time to test it out, I'm at school.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: