VB6 function to format floating point number rounded to specified number of significant figures

Hi Experts,

I am looking for a VB6 function that will format floating point numbers (say doubles) rounded to a specified number of significant figures.
I do not want scientific format.

Regards,
Leigh
LVL 1
LeighWardleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:

Dim a As Double
a = 12.333
MsgBox (FormatNumber(a, 2))
'gives Output as 12.33

Open in new window

0
LeighWardleAuthor Commented:
om_prakash_p:

That code does not work for all inputs, for example:

when a = -0.00000033358

the output should be -0.00000033, but your code gives 0.00
0
Om PrakashCommented:
The code simply formats the number and displays the number of 0's required after decimal.
So the output for
a = -0.00000033358
is
0.00

If you round variable "a"
MsgBox (Round(a, 8))
then you get the output you want.
Even this will not work for all cases. you need to handle those conditions separately.
0
ThomasianCommented:
Check this out

http://www.vbforums.com/showthread.php?t=269312

It seems to have issues with negative numbers though.


Remove this line from the code to fix it.

FormatSF = FormatSF * intSign

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeighWardleAuthor Commented:
Thomasian:

I had independently tried that code earlier, but had abandonded it because it did not work for negative numbers.
Thanks for the fix.

It also needed a fix for the case when the input is zero.
See my modified code below.
    'Returns input number rounded to specified number of significant figures.
    Function FormatSF(dblInput As Double, intSF As Integer) As String
    Dim intCorrPower As Integer         'Exponent used in rounding calculation
    Dim intSign As Integer              'Holds sign of dblInput since logs are used in calculations
    FormatSF = "0"                      'default
    '-- Store sign of dblInput --
    intSign = Sgn(dblInput)
    If dblInput <> 0# Then 'dblInput cannot be zero or Log10 will crash
        '-- Calculate exponent of dblInput --
        intCorrPower = Int(Log10(Abs(dblInput)))
        FormatSF = Round(dblInput * 10 ^ ((intSF - 1) - intCorrPower))   'integer value with no sig fig
        FormatSF = FormatSF * 10 ^ (intCorrPower - (intSF - 1))         'raise to original power
        '-- Reconsitute final answer --
        'FormatSF = FormatSF * intSign 'deactivated to fix negative values
        '-- Answer sometimes needs padding with 0s --
        If InStr(FormatSF, ".") = 0 Then
            If Len(FormatSF) < intSF Then
                FormatSF = Format(FormatSF, "##0." & String(intSF - Len(FormatSF), "0"))
            End If
        End If
        If intSF > 1 And Abs(FormatSF) < 1 Then
            Do Until Left(Right(FormatSF, intSF), 1) <> "0" And Left(Right(FormatSF, intSF), 1) <> "."
                FormatSF = FormatSF & "0"
            Loop
        End If
    End If
    End Function

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.