• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Rounding up to 3 significant figures

 
Ranch Hand
Posts: 85
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.
 
Bartender
Posts: 1558
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.
 
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: 1558
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
 
Marshal
Posts: 67945
258
  • 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?
 
Marshal
Posts: 25144
64
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.)
 
I'm still in control here. LOOK at this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!