Click to See Complete Forum and Search --> : Significant figures


bowza
March 14th, 2001, 05:59 AM
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

Clearcode
March 14th, 2001, 06:04 AM
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

CK Dixon
March 14th, 2001, 06:50 AM
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

bowza
March 14th, 2001, 07:23 AM
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

CK Dixon
March 14th, 2001, 07:34 AM
Use

Num2 = Int(Num1 * Pow10 +.5) / Pow10

to round.

Nanderson
March 14th, 2001, 06:20 PM
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

http://jsprod.odigo.com/share/servlets/OnLineR?userId=2986792&pId=odigo&design=3&tool=signature