aspose file tools*
The moose likes Beginning Java and the fly likes Rounding up to 3 significant figures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rounding up to 3 significant figures" Watch "Rounding up to 3 significant figures" New topic
Author

Rounding up to 3 significant figures

Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 72
Hi Ranchers, I'm trying to format a word to three significant figures but the methods round and ceil are not actually working out well. I'll want to make this variable z return 0.985. double z = 0.98477908; The round and ceil are returning 1 and 1 respectively which is not supposed to. Thanks in advance.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Hi Shamsudeen Akanbi,

Perhaps NumberFormat class might help you here.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 44
You can use the following code and hope it will work



Output is : 0.985
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Rajat Jindal wrote:You can use the following code and hope it will work

Hi Rajat,

Welcome to CodeRanch!

Please DontBeACodeMill so that people will get joy of finding things on their own
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Agree about not providing complete answers, but it is too late to change it now.

That is not rounding. That is displaying the number to 3 decimal places, not three significant figures. What would it display for this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

It's quite possible that Shamsudeen really meant "three decimal places" and not "three significant figures", though. You can't tell from the example given.

"Round to three significant figures" looks like a fairly hard problem, now that you bring up the point. At least, it doesn't have a simple solution like Decimal.format. (Not that I can see anyway.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rounding up to 3 significant figures