Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

significant digits function 2

Status
Not open for further replies.

choppysuey

Technical User
Mar 28, 2001
54
US
Does anyone know if Access 2003 has a significant digits function? i.e.
(3.0, 3) get 3.00
(3.1, 2) get 3.1
(0.2, 3) get 0.200
(0.21,3) get 0.210

I'm looking through the threads and help and haven't come across anything yet and was hoping someone had the answer.

Thanks
 
Thanks for the input. But even with the format function it seems that I will still have to write a fairly extensive user function and pass values to that function to get the functionality that I need.
 
What are you trying to accomplish here? Are you trying to format the value for display? Or are you trying to cut the actual value to a certain number of digits?

In other words...does functionname(0.21, 2) return a "formatted string" of 0.2 but the value is still 0.21 for math purposes, or should functionname(0.21, 2) return a numeric calue of 0.2?

These mean two TOTALLY different things and need to be considered before we can figure out how best to help.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
functionname(0.21, 2) states given a value of 0.21 and the #of significant digits to keep (2) in this case, I want the function to return 0.21 either text or numeric (prefered) doesn't matter. What I really care about is that on the report the number shows up as 0.21

If the value passed is (0.21, 3) I want the value to show up as 0.210 or if (3.0, 4) I want 3.000
(3, 5) I want 3.0000

Hope this is a clearer explanation of what I'm trying to do.
 
Well....It seems the distinction between string or numeric really didn't matter too much when I went to try to solve this. Here are the solutions. First one returns a string (which I would use in a report unless you are multiplying it by something else....all "display only" data should be string format). The second one returns a single numeric value.

Both take into account that if the value is less than 1, the leading zero is not considered a "significant digit". Hope that is what you wanted cause your sample demonstrated that.

Also, I didn't include any checking to see if the number of significant digits is at least log10 safe...in other words, if you say GetSigDigits(24.21, 1) it will return "2" and I don't think that is the correct answer. Let me know if you need something that meets that requirement. Again, I based this off your sample, which shows only ones digits before the decimal.

Code:
Public Function GetSigDigits(sngValue As Single, intdigits As Integer) As String

    GetSigDigits = Left(CStr(sngValue) & "0000000000", IIf(intdigits = 1, IIf(sngValue < 1, 3, 1), intdigits + IIf(sngValue < 1, 3, 1)))

End Function

Code:
Public Function GetSigDigitsNum(sngValue As Single, intdigits As Integer) As Single

    GetSigDigitsNum = CSng(Left(CStr(sngValue) & "0000000000", IIf(intdigits = 1, IIf(sngValue < 1, 3, 1), intdigits + IIf(sngValue < 1, 3, 1))))

End Function

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Wow..Robert, just got back into the office and saw your post. Am in the process of understanding and testing it. Here are some of the quarks that I've found so far:

Any integer value or someNumber.0 gives incorrect output i.e.:

sigdig(2, 4) should show 2.000 not 20000
sigdig(2.0, 4) should show 2.000 not 20000
sigdig(2.00,4) should show 2.000 not 20000
sigdig(2.000,4) should show 2.000 not 20000
sigdig(2.0000,4) should show 2.000 not 20000
sigdig(20, 4) should show 20.00 not 20000
sigdig(200,4) should show 200.0 not 20000

Also any value below 1 the function is tacking on an additional 0 i.e.:

sigdig(0.99, 4) should give 0.9900 not 0.99000
sigdig(0.999, 4) should give 0.9990 not 0.99900
sigdig(0.9999, 4) should give 0.9999 not 0.99990

I'll see if I can tweak your code or use it as a starting point and see if I can't get those issues worked out of it. Unfortunately coding comes very slowly to me! Thx for your help. It was extremely helpful. LOL..and if you happen to have the time to take a look at this again I wouldn't mind at all for the additional help!
 
A starting point:
Code:
Public Function SigDig(sngValue As Single, intDigits As Integer) As String
SigDig = Left(Format(sngValue, "0.000000000"), intDigits + IIf(sngValue < 1, 2, 1))
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I think PHV hit it on the head before I could review.....THANKS PHV!

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top