• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rounding up to 3 significant figures

 
Shamsudeen Akanbi
Ranch Hand
Posts: 83
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shamsudeen Akanbi,

Perhaps NumberFormat class might help you here.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the following code and hope it will work



Output is : 0.985
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48378
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20724
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic