Solved

VB Significant Digits function

Posted on 2003-11-09
8
863 Views
Last Modified: 2010-08-05
Does anyone have a visual basic function that will return a passed number with a requested number of significant digits?
For example:
X = 1.233412
Y = FixSigFigs(X,3) 'I want Y to have 3 significant digits.

Function FixSigFigs( NumberToFix as double, SigFigs as Integer)

0
Comment
Question by:kar8942
8 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 83 total points
Comment Utility
Round Function

Returns a number rounded to a specified number of decimal places.
Round(expression [,numdecimalplaces])

Dim MyVar, pi
pi = 3.14159
MyVar = Round(pi, 2) ' MyVar contains 3.14.

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 83 total points
Comment Utility
Public Function FixSigFigs(NumberToFix As Double, SigFigs As Integer) As Double
    Dim textNumber As String
    Dim decimalPlace As Integer
    Dim digitsToRight As Integer
   
    FixSigFigs = NumberToFix ' If no change, return original number
    textNumber = CStr(NumberToFix)
    decimalPlace = InStr(textNumber, ".")
    If decimalPlace > 0 Then
        digitsToRight = Len(textNumber) - decimalPlace
        If digitsToRight > SigFigs And SigFigs >= 0 Then
            FixSigFigs = CDbl(Left(textNumber, decimalPlace + SigFigs))
        End If
    End If
End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
You didn't specify as to whether you wanted rounding or not.  Use the Round() function, as AlexFM suggested, if you need rounding to occur.  Otherwise, use my function if you simply want to truncate the number to the requested number of digits.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Accepted Solution

by:
VK earned 84 total points
Comment Utility
hello kar8942 !

That isn't a simple task in general.
Of course you can use the built in round function of vb.
But this function isn't country-independent.

Example:

? round(1.145,2)
 1,14
? round(1.245,2)
 1,25

In germany for example round(1.145,2) should be 1.15.
Therefore i wrote a user defined function:

Public Function RoundIt(Number As Variant, ByVal NumDigitsAfterDecimal As Long) As Variant
    Select Case True
        Case Not IsNumeric(Number)
            RoundIt = Number
        Case Else
            NumDigitsAfterDecimal = 10 ^ NumDigitsAfterDecimal
            RoundIt = Int(CDec(CDec(0.5) + CDec(Number) * CDec(NumDigitsAfterDecimal))) / CDec(NumDigitsAfterDecimal)
    End Select
End Function

regards

V.K.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
What did you decide to use?
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
kar8942 got two ways: rounding and truncating. He should decide what he need. Without his response I think points should be splitted.
0
 

Author Comment

by:kar8942
Comment Utility
Neither answer actually worked, but I will split the points anyway. Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now