PDA

View Full Version : Decimal places



Coolmo
2004-12-10, 03:51 PM
Here's a big ol' problem I'm having with VBA. When I try to do simple calculations with variables I always get a wacky answer with an "E" in it and the decimal has now shifted. I know what the "E" is and what it means but how do I get the answer to show correct?

Example:
X = 0.0208
Y = 0.0200
X - Y = 7.99999999E-04

ike.eisenstadt
2004-12-10, 04:11 PM
You've probly declared the variable as variants---try declareing them as single or double


Like

Sub test()
Dim x, y, z As Single
x = 0.0208
y = 0.02

z = x - y
Debug.Print z

End Sub

this will give an answer of 0.0008 instead of 7.99999999999999E-04

Ed Jobe
2004-12-10, 04:21 PM
Its always best to declare variables as the type you will use. Not only for the reason you show, but also to save memory (variants require more than other types). Not quite as important these days, but it can add up.

Another tip:

Dim vars separately, e.g.
Dim strFirstName, strLastName As String 'You think both are strings? strFirstName is a variant.

Use:
Dim strFirstName As String, strLastName As String

or my pref
Dim strFirstName As String
Dim strLastName As String

Coolmo
2004-12-10, 04:43 PM
I tried to declare them with type-declaration characters (where ! = single) and it still gave me goofy answers.

x! = 0.0208
y! = 0.02
z! = x! - y!

Your way, by using "Dim X as single", worked correctly. Why would my way not work? I thought that's what type-declaration characters did.

ike.eisenstadt
2004-12-10, 04:52 PM
I haven't used type declaration charactors since pascal, so I couldn't tell ya.

I always try to explicitly declare variables and types in the sub's header--

Ed Jobe
2004-12-10, 05:14 PM
The type decl comes after the number, not the var name.

Coolmo
2004-12-10, 08:24 PM
According to a Visual Basic FAQ website and my VB6 book......

"Type-declaration characters can be used anywhere, not just in a Dim statement. They not only serve to declare a variable's type, but also to help the reader recognize a variable's type when it's used in the code. "

Example:

Dim I As Integer
Dim I%

Coolmo
2005-01-28, 07:04 PM
I hate to bring this subject back again but I'm still getting the "E" exponent thing for certain answers. I've declared everyting as either a single or a double before any operators are used but still some numbers are coming back with the "E" in the answer (i.e. 0.05 = 5.0E-02)
The numbers I have going into the equations are coordinate points from AutoCAD so the numbers are all out to the 7th or 8th decimal place (205.0000001) . Could this be the problem?

mtuersley
2005-01-31, 02:55 PM
For the majority of AutoCAD related things, you should only be using integer and double. Let's try a different route, here. What is it you are trying to do? What's your end goal? Maybe then we can post an example for you to follow.

Coolmo
2005-01-31, 07:14 PM
One of the most basic examples of what I'm doing is taking the coordinates of a line (which when listed are carried out to 8 or 9 decimal places by VBA) and using the X and Y of the coordinates to obtain the slope of the line, and then pass that slope to a label or textbox. When I do this, for some reason the majority of the "answers" given by VBA are OK but occasionally the answers will appear with the "E" and the decimal place is off by that exponent. I've declared everything either a single or a double and a mix of the two and it still seems to add the "E" every once in awhile.

I will add that when I query the line object I then put the retrieved X,Y coordinates into a listbox first for later use and then I use those numbers from the listbox to start all the calculations. I use the Val(string) method to turn the coordinate string into a number that can be used.

jwanstaett
2005-01-31, 07:46 PM
when you display a number as text use the VBA format function that way you can control the way the number look

exp:
MyStr = Format(7.999E-4, "##,##0.0000") ' Returns "0.0008".
MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40".
MyStr = Format(334.9, "###0.00") ' Returns "334.90".
MyStr = Format(5, "0.00%") ' Returns "500.00%".
MyStr = Format("HELLO", "<") ' Returns "hello".

Coolmo
2005-01-31, 09:12 PM
That's awesome! I didn't even know that existed. I appreciate it.