|
-
March 14th, 2001, 06:59 AM
#1
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
-
March 14th, 2001, 07:04 AM
#2
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
-
March 14th, 2001, 07:50 AM
#3
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
-
March 14th, 2001, 08:23 AM
#4
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
-
March 14th, 2001, 08:34 AM
#5
Re: Significant figures
Use
Num2 = Int(Num1 * Pow10 +.5) / Pow10
to round.
-
March 14th, 2001, 07:20 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|