Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB Significant Digits function

Posted on 2003-11-09
8
873 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
ID: 9709953
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
ID: 9709955
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
ID: 9710584
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 6

Accepted Solution

by:
VK earned 84 total points
ID: 9714620
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
ID: 9872472
What did you decide to use?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10163002
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
ID: 10164264
Neither answer actually worked, but I will split the points anyway. Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 76
MS Access Search and Replace Using VBA 6 84
Getting warning: You are about to delete 1 row(s) 9 52
Excel Automation VBA 19 90
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 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