Round problem

  • Thread starter Thread starter Harry
  • Start date Start date
H

Harry

I need a function f which rounds to a given number of
signicant digits, like:

f(pi(),3) =3.14
f(pi(),5) =3.1415
f(100*pi(),3)=314
f(100*pi(),5)=314.15

How do I get this done?
 
To round the number:
=ROUND(number,digits-(1+INT(LOG(number))))
or to truncate it (as in your example):
=TRUNC(number,digits-(1+INT(LOG(number))))
 
Thanks, your idea works when modified to
ROUND(number,digits-(1+INT(LOG(ABS(number)))))
TRUNC(number,digits-(1+INT(LOG(ABS(number)))))
because my numbers might be negative.

Regards, Harry
 
Thanks, your idea works when modified to
ROUND(number,digits-(1+INT(LOG(ABS(number)))))
TRUNC(number,digits-(1+INT(LOG(ABS(number)))))
because my numbers might be negative. ....

There's an easier way involving fewer function calls.

=--TEXT(number,REPT("0",digits)&"E+000")

This also has the advantage that it works when number is zero.
 
But that doesn't handle decimal places?

Harlan Grove said:
There's an easier way involving fewer function calls.

=--TEXT(number,REPT("0",digits)&"E+000")

This also has the advantage that it works when number is zero.
 
Stephen Bye said:
But that doesn't handle decimal places?
....

Oops. Forgot the leading decimal point in the format. Make that

=--TEXT(number,"."&REPT("0",digits)&"E+000")
 
Back
Top