Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I am posting a code by @Rick Rothstein on significant figures rounding.
To a large extent, the code meets the requirement but is failing with trailing zeroes in the decimal part.
For instance, "12345.0067890" (not 12345.006789) has 12-significant figures.
If we are rounding this number to exactly 12 sig. figures, we should get 12345.0067890 and not 12345.006789 (which has 11 sig. figures).
i.e. RoundSignificantFigures("12345.0067890", 12) should output "12345.0067890".
How can the code be modified to incorporate this aspect?
To a large extent, the code meets the requirement but is failing with trailing zeroes in the decimal part.
For instance, "12345.0067890" (not 12345.006789) has 12-significant figures.
If we are rounding this number to exactly 12 sig. figures, we should get 12345.0067890 and not 12345.006789 (which has 11 sig. figures).
i.e. RoundSignificantFigures("12345.0067890", 12) should output "12345.0067890".
How can the code be modified to incorporate this aspect?
VBA Code:
Function RoundSignificantFigures(Value As Variant, Significance As Long) As Double
Dim Num As String
Dim Parts() As String
Num = Format(Value, "0.##############################e+0;;0")
Parts = Split(CStr(Num), "E", , vbTextCompare)
If CDbl(Parts(0)) = 0 Then
RoundSignificantFigures = 0
Else
RoundSignificantFigures = CDbl(Format(Parts(0), "0" & _
Left(".", -(Significance <> 0)) & _
String(Significance - 1, "0")) & "E" & Parts(1))
End If
End Function