Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Round to the nearest 10th?

 
Jerome Butler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Java 5 on WinXP...

How do I round the following to the nearest tenth?

I tried Math.round() but it didn't work...

Here's my code:



How can I make it display 10.50?

Thanks,

Jerome
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this,

double value = 10.499

System.out.println((Math.round(value*10))/10);
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try the java.text.DecimalFormat class:



Output:
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.round() converts from double to long, and from float to int, as an alternate to casting (which always truncates).

The suggestion to use DecimalFormat is an excellant one. For anything having to do with displaying data to the user, it makes sense since it converts the number into a string. The only time i wouldn't use it is if you need the numeric value of the converted number for an math-based algorithm. You could still use DecimalFormat but converting to string then back to a number is extra work.
[ February 04, 2006: Message edited by: Scott Selikoff ]
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jerome Butler:
I am using Java 5 on WinXP...

How do I round the following to the nearest tenth?

I tried Math.round() but it didn't work...

Here's my code:



How can I make it display 10.50?

Maybe that was just a typo, but 10.50 is the nearest hundredth, not the nearest tenth. (10.5)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JDK 5, you can use

or

depending on whether you really want tenths or hundreds. See here for more info.
[ February 04, 2006: Message edited by: Jim Yingst ]
 
Jerome Butler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, Jesus, and others,

Thanks for the cool suggestions...

What about when it comes to trying to round to the nearest .05?

Should I do it like this:




Hope to hear a response,
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jerome Butler:
Jim, Jesus, and others,

Thanks for the cool suggestions...

What about when it comes to trying to round to the nearest .05?




Should I do it like this?

This won't work. Math.round(double) rounds double to the nearest long. multiplying by .05 is equivalent to dividing by twenty. Quick calculations suggest, then that any
I'll track the steps in this algorithm with a range of values:
values from -10 to 9
*.05 -.5 to .45
Math.round 0 to 0
/.05 0 to 0
So anything between -10 and 9.9999 will be mapped to 0.

You want things to go to the nearest .05 - here's how one might go to the nearest .5.

Math.round(value * 2) /2

Now let's trace a few values:

I haven't checked whether it works for 1.5 itself, and you will need to make it apply to your situation a little more directly.

HTH

-Adam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic