Jennifer Murphy said:
I think my first example is a good illustration for this,
but to reiterate, since the result is 0.000083395880243516,
I would want it formatted as "0.000083".
Yes, I think all of your examples have been quite clear. Moreover, I think
it is clear that you want the result __formatted__ (displayed) with the
specified number of significant digits. You wrote: "I want to leave the
value unchanged and only *format* it".
You now realize that you must change the value (i.e. have a helper cell with
a formula that changes the value). But I suspect you are relying on the
behavior of the General format to display the specified number of
significant digits.
You can only take that so far. For example, if you wanted
0.000083395880243516 formatted to 6 significant digits in a cell with the
General format, formulas that return a numeric value rounded to 6
significant digits will display 8.33959E-05. I presume you want
0.0000833959.
I believe that requires a text result. The following user-defined function
should return text that formats the number to the specified significant
digits. You can format the cell with Horizontal Right Alignment.
If A1 contains the original value and A2 contains the number of significant
digits, then A3 might be the formula =vformat(A1,A2). If you want to use A3
as a number elsewhere, often it will work; e.g. =A3*2. But more generally,
use --A3. The double-negative (--) converts the numeric text to a number in
contexts where it would not otherwise be converted, e.g. IF(--A3>1,...)
The UDF....
Option Explicit
Function vformat(x As Double, sig As Long) As String
Dim s As String, p
If sig <= 0 Then sig = 1 _
Else If sig > 15 Then sig = 15
' create p(0 to 1) as Variant regardless of how
' Option Base is set
p = Split(Format(Abs(x), _
"." & String(sig, "0") & "E+00"), "E")
If p(1) <= 0 Then s = "0" _
Else s = Mid(p(0), 2, IIf(sig < p(1), sig, p(1)))
If p(1) > sig Then
s = s & String(p(1) - sig, "0")
ElseIf p(1) < sig Then
s = s & "."
If p(1) > 0 Then s = s & Right(p(0), sig - p(1)) _
Else s = s & String(-p(1), "0") & Mid(p(0), 2, 99)
End If
If x >= 0 Then vformat = s _
Else vformat = "-" & s
End Function