repost from scripting about sig fig functions

M

Mike D

I need a function that can format a number with the appropriate number of sig
figs. Ideallty a function that takes two values the value to be converted
and the number of sig figs I want it formatted to. Has anyone done this?
Any leads on how to? I have been searching the net and haven't found
anything yet.


Thanks
Mike
 
B

Bob Barrows [MVP]

Mike said:
I need a function that can format a number with the appropriate
number of sig figs. Ideallty a function that takes two values the
value to be converted and the number of sig figs I want it formatted
to. Has anyone done this? Any leads on how to? I have been
searching the net and haven't found anything yet.
"sig figs"? Do you mean "significant figures"? In the true scientific sense
of the word? Perhaps you should post some examples so we are all on the same
page. Show some numbers and then show the output you want when passing
different "sig fig" values.

Bob Barrows
 
R

Ray Costanzo [MVP]

It's been about 13 years since I've heard anyone mention "sig figs,"
assuming your lingo is what I think it is. (Significant digits in
scientific math?)

Like, do you mean this?

3.45 * 1.2 = 4.1
as opposed to
3.45 * 1.2 = 4.14

If so, how do you intend to pass the arguments to the function? An array of
numbers used in an arithmetic operation and the result? Like, would you do:

Dim aNumbersUsed(1)
Dim dblResult
aNumbersUsed(0) = "3.45" '''pass as strings, not numerics
aNumbersUsed(1) = "1.2"
dblResult = 4.14 ''or the arithmetic operation here
Response.Write SigFig(aNumbersUsed, dblResult)


Function SigFig(ArrayOfNumbers, Result)
Dim i, s
Dim iSigCount
Dim iMinSigs
Dim sResult, aResult, iInteger, iDecimal

iMinSigs = 99
For i = 0 To UBound(ArrayOfNumbers, 1)
s = ArrayOfNumbers(i)
If Int(s) = CDbl(s) Then ''no decimals
''cdbl to prevent leading zeros from counting,
''although I can't see why they'd be there. :]
iSigCount = Len(CStr(CDbl(s)))
Else ''decimal number
''get length before and after decimal
iSigCount = Len(CStr(Int(s))) + Len(CStr(Split(s, ".")(1)))
End If

''If the sigcount is lower than what it was
''the last time through the loop, take that as
''the current number of significant digits to return
If iSigCount < iMinSigs Then iMinSigs = iSigCount
Next

''Now we presumably have the correct number of significant digits, so
''we can take the result and signify it
''I do not remember all the specific rules
''of significant digits.
''I imagine that step two will involve
''splitting the result that was passed by a the . character
''and testing LENs and padding with zeros or rounding

''For now, I'll just return the number of significant digits
If iMinSigs = 99 Then
''no significant digits found
SigFig = "There was no significance in what you passed to this
function."
Exit Function
Else
SigFig = iMinSigs
End If
End Function


Ray at work
 
M

Mike D

Bob Barrows said:
"sig figs"? Do you mean "significant figures"? In the true scientific sense
of the word?

Yes, all the things we hated about science.

Example
0.099 is 2 sig figs
0.000009 is 1
..997 is 3

I need to take a number like 0.0000099734 and convert to a wide variety of
sig figs. Meaning some variables will need 2, some 3 etc. So I can't write
it for only one level. I have since found
http://www.vbforums.com/showthread.php?s=&threadid=269312

and am testing it.

Thanks for the replies




Perhaps you should post some examples so we are all on the same
 
R

Ray Costanzo [MVP]

Example
0.099 is 2 sig figs
0.000009 is 1

I don't remember leading zeros to the right of the decimal not counting as
significant. If that's that's one of the rules of significant digits, that
doesn't make any sense to me. Why are those zeros any less significant than
other numbers when in that position? No wonder I failed chemistry. ;]
.997 is 3

That makes sense. But, .007 is just as precise a .997. Maybe I'll see if I
can find a Physics textbook or something and read it.


Ray at work
 
B

Bob Barrows [MVP]

Ray said:
I don't remember leading zeros to the right of the decimal not
counting as significant.

Which explains my request for examples. I think he's got a solution, so I'll
wait for him to follow-up before spending any more time on this.

Bob
 
B

Bob Barrows [MVP]

Ray said:
Example
0.099 is 2 sig figs
0.000009 is 1

I don't remember leading zeros to the right of the decimal not
counting as significant. If that's that's one of the rules of
significant digits, that doesn't make any sense to me. Why are those
zeros any less significant than other numbers when in that position?
No wonder I failed chemistry. ;]
.997 is 3

That makes sense. But, .007 is just as precise a .997. Maybe I'll
see if I can find a Physics textbook or something and read it.
..007 = 0.7E-2 so it has one sig fig

Bob
 
D

Dave Anderson

Ray said:
That makes sense. But, .007 is just as precise a .997.

In addition, yes. In multiplication, no.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
M

Mike D

Here is a site that shows some examples

http://science.widener.edu/svb/tutorial/sigfigures.html

Yes, I have a solution but I am now fighting my test server it is giving a
"HTTP 405 - Resource not allowed" error when doing a simple post.

Mike




Ray Costanzo said:
Example
0.099 is 2 sig figs
0.000009 is 1

I don't remember leading zeros to the right of the decimal not counting as
significant. If that's that's one of the rules of significant digits, that
doesn't make any sense to me. Why are those zeros any less significant than
other numbers when in that position? No wonder I failed chemistry. ;]
.997 is 3

That makes sense. But, .007 is just as precise a .997. Maybe I'll see if I
can find a Physics textbook or something and read it.


Ray at work
 
R

Ray Costanzo [MVP]

Try specifying the page name to wich you're submitting the form. Even if
it's default.asp, which I imagine it is, specify that in the form action.

<form method="post" action="default.asp" ...>

You cannot do something like:

<form method="post"> (If the page is loaded as the default document and
you're at a URL with no document specified)

or

<form method="post" action="/directoryName/">

Ray at work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top