CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2000
    Location
    England
    Posts
    5

    Significant figures

    Hello

    Can anyone suggest a simple method to display numbers to a set number of significant figures in vb6?

    Example (a) variable is 72.29115, I would like to display to 5 s.f. So variable becomes 72.291
    Example (b) 125.8353 to 5 s.f. I would like 125.84

    Ok simple enough to use trim and left 6. As these numbers in many instances are written to a text file, I need to format them, so "##.###" in some cases and "###.##" in others.

    My question is, since I have many instances where this occurs is there a simple way to do this format other than a function, any ideas? I do need to keep this variable real for calculations

    Thanks

    bowza



  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Significant figures


    sValue = Left$(Format$(nValue,"#.#####"),IIf(Instr(Format$(nValue,"#.#####"),"."),6,5))




    That should do it - six digits if one of them is a decimal, 5 if it isn't.
    Note that the hard coded "." is not good enough if your app is international - you'll have to pick up the decimal separator from the control panel settings.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Jun 2000
    Location
    Nepal
    Posts
    108

    Re: Significant figures

    Here's one solution



    private Sub Command1_Click()

    Dim Num1 as Single
    Dim Num2 as Single
    Dim Digits as Integer
    Dim Pow10 as Long

    Num1 = 172.29115

    Digits = Int((Log(Num1) / Log(10))) + 1
    Pow10 = 10 ^ (5 - Digits)

    Num2 = Int(Num1 * Pow10) / Pow10

    MsgBox Num2
    End Sub






  4. #4
    Join Date
    Oct 2000
    Location
    England
    Posts
    5

    Re: Significant figures

    Thank you for the replies so far, unfortunately neither of your solutions accounts for rounding up.

    Example 123.4567 to 5 sf is 123.46 not 123.45, I should have pointed this out initially, since it is the reason why the trim left 6 option is unsuitable.

    bowza

  5. #5
    Join Date
    Jun 2000
    Location
    Nepal
    Posts
    108

    Re: Significant figures

    Use

    Num2 = Int(Num1 * Pow10 +.5) / Pow10

    to round.


  6. #6
    Join Date
    Dec 2000
    Location
    Los Angeles, CA
    Posts
    34

    Re: Significant figures

    How about something like this?


    dblValue = Round(dblValue, 5 - len(Round(dblValue, 0)))



    This should take care of your rounding problem and always have 5 significant digits.

    Nathan


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured