Significant Figures Rounding VBA

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. 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?

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
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,215,857
Messages
6,127,372
Members
449,381
Latest member
Aircuart

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top