Win a copy of Design for the Mind this week in the Design forum!

# Rounding up to 3 significant figures

Shamsudeen Akanbi
Ranch Hand
Posts: 83
1
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
Hi Shamsudeen Akanbi,

Rajat Jindal
Ranch Hand
Posts: 44
You can use the following code and hope it will work

Output is : 0.985

Anayonkar Shivalkar
Bartender
Posts: 1557
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
Posts: 48652
56
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
Posts: 20955
31
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.)