# Thread: need 2 decimal places

1. ## need 2 decimal places

I have a program that calculates cubic inch requirements for choosing an electrical box size based on entries on my box sizing form. Problem is the form calculates but does not go to 2 decimal places which will cause an improper cubic capacity to select a box. I am a novice in VB6

here is the code snippet from my program:

If Combo6.Text = "12" Then
Txte = 2.25 * Txthm.Text
Txtsize(7).Text = Txte
Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti

the values entered are:
(Txtsize(7).Text) = 4.5
txtf = 4
txth =2.25
txti = 0.5

Txttotcubic.Text = 11. (it should be 11.25)

when put a watch on the code, I get out context for the Txttotcubic.Text. I have Dim totcubic As Double

Any idea on how to get the full answer to 2 decimal places ?  Reply With Quote

2. ## Re: need 2 decimal places

Thinking that Txttotcubic is probably returning the right result.

You mention that Txtsize(7).Text contains 4.5, but in your code just before the formula, you overwrite it with: 2.25 * Txthm.Text

You also didn't mention what the other variables contain: X, b, c, d, txtG, Txthm.Text or what variable type you declared any of the non-textbox variables.

And finally, you said, "I have Dim totcubic As Double". Well that variable isn't even used in the formula your provided.

Bottom line, we can't confirm or deny that Txttotcubic is returning the correct value or truncating the decimal portion. Just not enough info  Reply With Quote

3. ## Re: need 2 decimal places

I would assign all the values from the textfiles into the right dimmed variable.
example:

Txthm = val(Txthm.Text)
Txte = 2.25 * Txthm

example:

Tx7 = val(Txtsize(7).Text)
Txbcd = X + b + c + d
TxC = 2.25 * Txbcd
TotCubic = TxC + Tx7 + txtf + txtG + txth + txti
Txttotcubic.Text = TotCubic

and so on,
this so that calculation is done properly in each step.
and each one is dimmed correctly, this to avoid rounding or type conversions since you didnt include that in the formula.  Reply With Quote

4. ## Re: need 2 decimal places

I have the default settings. Therefore, a period is used as a separator. If you use a comma, the following digits will be ignored.
When working with numbers, I made a simple subroutine that validates information entered into a text box, ignores characters and changes commas to periods.  Reply With Quote

5. ## Re: need 2 decimal places

Don't use the ancient Val() unless you really mean to. It always uses the Invariant Locale.

Instead we have things like CSng(), CDbl(), etc. which are locale aware. No need for funky gymnastics like replacing commas by periods. Just do it right in the first place.

Implicit coercion can be deadly, and it appears to be relied upon without awareness all over the place in that code.

Variables are not "dimmed" but dimensioned (which is where Dim gets its name).  Reply With Quote

6. ## Re: need 2 decimal places

the Val() is used to avoid crashes. sure you can avoid that by having functions to prevent invalid values.
a programmer need to grow in his own pace and Val is not in any way bad.
also, if u have dimmed the variable, the Val will convert into that, the only reason to use CSng/CDbl etc, is when you want to convert or you are a speed freak, and I mean, if you are not dealing with heavy calculation theres no need for that.

you are both dimensioned "and" type assigned when using Dim. do not make it more complicated than it is.  Reply With Quote

7. ## Re: need 2 decimal places

Well looking at your code the values you gave would add up to 11.25 but you are also adding other things to that on the same line which you have not given us a value for. You also did not indicate what those vars are defined as. for example you say
Code:
`txth =2.25`
but if txth is defined as an integer it will be 2.0 instead.  Reply With Quote

8. ## Re: need 2 decimal places

but if txth is defined as an integer it will be 2.0 instead

actually, it will be 2 instead  Reply With Quote

9. ## Re: need 2 decimal places

Well, I am glad you said you are a novice at VB6...so am I (compared to many on this site), but, what we all need, as LaVolpe said, is more information.

It would be helpful if you could identify which of those 'things' you have in your code are textboxes or variables. Also, for any variables used, show how they are declared (BY THE WAY, like many beginners, you may have not declared Option Explicit (On) in your form. Please do that, THEN step through your code...it will show you what variables you have not declared, and what ones you have, you can see their values.)

Also, if you could simply give us the NUMBERS which you want to calculate to be cubic inches, I am sure many can help more quickly...

usually to calculate CI, you need a height, width and depth...what numbers are YOU trying to use to determine CI.  Reply With Quote

10. ## Re: need 2 decimal places Originally Posted by dilettante Don't use the ancient Val() unless you really mean to. It always uses the Invariant Locale.

Instead we have things like CSng(), CDbl(), etc. which are locale aware. No need for funky gymnastics like replacing commas by periods. Just do it right in the first place.
Try the simple code yourself:
Code:
```Dim First As Double
'.....................
First = CDbl(Text1.Text)```
Try entering numbers with a comma and a period in the text box. In one case, you will receive a message about data type mismatch and the compiled program will simply "crash". The same will happen if at least one character is entered.
You can't do without funky gymnastics if the program is written not only for yourself.  Reply With Quote

11. ## Re: need 2 decimal places

thanks to all for looking at this, still returning (11.)

here is the code snippet from my program:

If Combo6.Text = "12" Then
Txte = 2.25 * Txthm.Text
Txtsize(7).Text = Txte
Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti

the values entered are:
(Txtsize(7).Text) = 4.5
txtf = 4
txth =2.25
txti = 0.5

Txttotcubic.Text = 11. (it should be 11.25)

What the program does is take values already calculated in cubic inches (ie. a number 12 conductor is 2.25 cu.in.),and then add the cubic inches for a total volume in the electrical box being sized. A Electrical box can have conductors, grounding conductors, yokes, clamps ,fixture studs , hickeys, which take up space in the box. There are rules of how many deductions are required. So depending on what is in the box, the deduction is included in the formula for Txttotcubic.Txt.

so for this example: We have the following entries
because these variables , e, f, h, i can have decimal values, should they be Dim As Double?
[/B][/B] Note (Txtsize(7).Text)= txte
(Txtsize(7).Text) = 4.5, 2 x 2.25 = 2 number 12 conductors (2.25 cu in)
txtf = 4, 2 x2 = 2 number 14 conductors (2 cu in)
txth =2.25, 2.25 x 1= 1 number 12 grounding conductor (2.25 cu in)
txti = 0.5, 2x0.25= 1 additional number 14 grounding conductor (2 cu in)

(X + b + c + d) are deductions for yokes, clamps ,fixture studs , hickeys if found in the box, if not in box no entry. So dim as Single or Integer?

here are my current Dims
Dim d As Double, gc As Double
Dim e As Double, hm As Double
Dim f As Single, hm1 As Double
Dim g As Double, hm2 As Double
Dim h As Double, hm3 As Double
Dim i As Double
'Dim totcubic As Double 'beginnig was as integer
'Txttotcubic.Text = totcubic
Dim Txtboxcube As Integer  Reply With Quote

12. ## Re: need 2 decimal places

I think you confused us even more. It appears you were settng the formula result to totcubic? If so and it was declared as Integer, that explains the rounding. And now you are saying that when you assign your formula directly to a textbox, the result is still rounded down? If so...

in this formula: Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti
please tell us what each member's value in that formula is when you get the unexpected return value.

P.S. I assume X, b & d are not declared?
As a novice, I think you should get in the habit of ensuring the following term is at the top of every code page: Option Explicit. When compiling, this prevents usage of variables that are undeclared. Undeclared variables often result in using typos in code and are not too easy to spot/troubleshoot.  Reply With Quote

13. ## Re: need 2 decimal places Originally Posted by SamOscarBrown actually, it will be 2 instead[/COLOR]
Yeah I know. I just put the .0 on there to make it clear that the 1/4 would be dropped.  Reply With Quote

14. ## Re: need 2 decimal places

I'm a little confused with this too apparently totcubic is not used
Note I had a friend code this several years ago and trying to update it.

in this formula: Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti

values (no entry for each) + (4.5) + (4) + (no entry) + (2.25) + (0.5)

Txttotcubic.Text = 11. should be 11.25  Reply With Quote

15. ## Re: need 2 decimal places

You show dim for f,g,h,i but the vars you are using are txtf, txtg, txth, txti and you do not show a value for txtg.
Given what you have show values for the only way that would be 11.25 would be if 2.25*(x+b+c+d)+txtg=0  Reply With Quote

16. ## Re: need 2 decimal places

Okay...here is a SAMPLE of what you are trying to do:

Code:
```Option Explicit
Dim a As Double
Dim b As Double
Dim c As Double
Dim d As Double
Dim txtf As Double
Dim txth As Double
Dim txti As Double

Private Sub Command2_Click()
txtf = 4
txth = 2.25
txti = 2

a = 0
b = 0
c = 0
d = 0
Txttotcubic.Text = CStr((2.25 * (a + b + c + d)) + CInt(txtNumberOf12Conducters.Text) + CInt(txtNumberOf14Conducters.Text * txti) + txtf + txth)
'txtNumberOf12Conducters = 2 and txtNumberOf14Conducters = 1
End Sub```
NOTICE---in your description of txti, you have: txti = 0.5, 2x0.25= 1 additional number 14 grounding conductor (2 cu in) ---which is it, 2 ci or .5 ci? ( my txtNumberof14Conductors.text = 1)
BUT, if you have just 1 #14 conductor and which you say is TWO (2) cu inches, not .25, then txti needs to equal 2, not .5 and the calculation above would be 10.25, not 11.25

If you have an ADDITIONAL #14 (meaning now 2????), then you'd need (in my example) txtNumberof14conductors.text to be "2", and the result would be 12.25

So, VERY confusing as to numbers of 12 and 14 conductors in this box.  Reply With Quote

17. ## Re: need 2 decimal places

I haven't studied your code in great detail. And that's because it looks like a bit of a mess, and also, it's not in the [code]the code lines[/code] tags. Those are easily placed around your code by using the # button on the little VBForums post editor, or you can just type them in when you're composing your post.

But, just upon a quick perusal, I don't see where the following variables are declared: X, b, c, d, txtf, txtG, txth, txti

If any one of those is declared as an Integer (or Long, or Byte), that could cause your problem. Also, in post #11, you showed us that Txtboxcube is declared as Integer.

Also, at least for me, it's a common habit to do all my calculations in declared variables. And then, the very last step is to put it back into a TextBox (possibly using the Format\$ function). Using TextBoxes as operational variables seems like a very bad habit to me. In fact, even initially, if the TextBoxes are suppose to have numbers, I'll pull those values out (putting them into declared variables) before I even begin any of my operations. That way, it's just MUCH easier to figure out what's going on when I step through my code.

Good Luck,
Elroy  Reply With Quote

18. ## Re: need 2 decimal places

here an example how things can change depending on what u do:
Dim Txte as Double

Code:
```Txthm = 11,5

Txte = 2.25 * Txthm.Text
= 25,875

Txte = 2.25 * Val(Txthm.Text)
= 24,75

Txte = 2.25 * CDbl(Txthm.Text)
= 25,875```
-----------------------------

Code:
```Txthm = 11.5

Txte = 2.25 * Txthm.Text
= Type Mismatch

Txte = 2.25 * Val(Txthm.Text)
= 25,875

Txte = 2.25 * CDbl(Txthm.Text)
= Type Mismatch```  Reply With Quote

19. ## Re: need 2 decimal places

you are correct the post did line up as I wished.

in this formula: Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti

2.25 * (X + b + c + d) (no entry for each)

(Txtsize(7).Text) =4.5

txtf = 4
txtG = no entry
txth = 2.25
txti = 0.5

hope this clears up the entries
So it sounds like txti should be Dim As Double rather than Dim i As Double and all the others?  Reply With Quote

20. ## Re: need 2 decimal places

@baka... ?????

Code:
```Private Sub Command1_Click()

Dim txte As Double

txthm.Text = 11.5

txte = 2.25 * txthm.Text
Debug.Print txte

txte = 2.25 * Val(txthm.Text)
Debug.Print txte

txte = 2.25 * CDbl(txthm.Text)
Debug.Print txte

End Sub```
works everytime...NOTICE I didn't let VB default to txthm's text value, but instead (for clarity, especially for newcomers) used txthm.TEXT

BUT, this is not helping OP with his issue  Reply With Quote

21. ## Re: need 2 decimal places

Are you sure electric that 11.25 is the correct calculation for that box? I see issues with your description of txti (and I am assuming it is a VARIABLE, not a TextBox (see latest from Elroy above)  Reply With Quote

22. ## Re: need 2 decimal places

I get Type Mismatch. u need to input 11.5 inside the textbox, not assign like u do inside the command button. try again and u will see.

yes it does, it help him understand how it works, depending where the value is taken.
everything is about learning, and if OP is new at programming, even this part is good to know.

also I hate to give him full working code, instead I want him to understand one part at a time and try again, if failing and not sure what to do, he post again asking for the next part to work.
right now he want everything to work, its a mess. instead of splitting up the code and see if each part is working, like u do when u are debugging a program that show errors, u go part to part and check if its working until u find the culprit  Reply With Quote

23. ## Re: need 2 decimal places

from the replies I see I have several problems.

The calculation pen and paper is 11.25

The txti is a variable not a text box but it is not dimensioned

however i is dimensioned

so I need to sort out and dimension the variables correctly for a start and check what is textbox or a variable

Thanks it's getting more clear  Reply With Quote

24. ## Re: need 2 decimal places

I am learning and step by step is best appreciate everyones help thanks  Reply With Quote

25. ## Re: need 2 decimal places

you need to Dim txti as Double

Although many do not follow good coding techniques, including myself, as far as naming conventions are concerned, but when one starts a 'thing' with "txt", it USUALLY means it is the beginning of a name of a TextBox. That confused many of us on your posts. You do not dim textboxes, but you do dim variables placed into them (unless a user types/copy-pastes something into them).

you don't even USE i, so why dim it?

if your txti is a variable as you say, it might behoove you to use a name convention like dConductorSize, where 'd' indicates a Double and the rest is a quasi description of what the variable is used for. Not required, but good practice...i for integer, s for string, etc, etc. Makes it easier for others to decipher your code.  Reply With Quote

26. ## Re: need 2 decimal places

@baka....don't know what you're doing differently, but when I comment out the line that 'sets' txthm to 11.5, and then type "11.5" into the textbox and run the command button, all three debugs print "25.875" (w/o the quotes)  Reply With Quote

27. ## Re: need 2 decimal places Originally Posted by Electric Chuck you are correct the post did line up as I wished.

in this formula: Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti

2.25 * (X + b + c + d) (no entry for each)

(Txtsize(7).Text) =4.5

txtf = 4
txtG = no entry
txth = 2.25
txti = 0.5

hope this clears up the entries
So it sounds like txti should be Dim As Double rather than Dim i As Double and all the others?
This may sound unconventional, but one way to attack this is to take the calculation:

Code:
`Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti`
and replace all variables with their "known" values.

Code:
`Txttotcubic.Text = 2.25 * (0 + 0 + 0 + 0) + (4.5) + 4 + 0 + 2.25 + 0.5`
Test it out, see what you get. Then, starting from left to right, re-introduce one variable at a time and test again.

Code:
`Txttotcubic.Text = 2.25 * (X + 0 + 0 + 0) + (4.5) + 4 + 0 + 2.25 + 0.5`
Then

Code:
`Txttotcubic.Text = 2.25 * (X + b + 0 + 0) + (4.5) + 4 + 0 + 2.25 + 0.5`
And so on.

Assuming that the result is correct with all literal values and it "breaks" after one of the variables is re-added, then look at how that variable is declared.

Good luck.  Reply With Quote

28. ## Re: need 2 decimal places

thanks good suggestion, as I said before I'm updating this program written by friend several years ago and did not realize that it had so many problems.

if the code is confusing to the experts no wonder a novice like me is having trouble trying to make it work correctly.

I am going take all help and clean it up

I'm vision impaired and going to doc so won't be able to respond quickly after the appt please keep suggestions coming thanks  Reply With Quote

29. ## Re: need 2 decimal places

Suggestion: while cleaning up the code, give your variables meaningful names. x, c, d, etc isn't very descriptive. Avoid naming textboxes with "txt" prefixes and also creating variables with "txt" prefixes. It makes it harder, when revisiting the code down the road, to know which is coming from user input and which are calculated variables.

Oh and while you're also at it, use Option Explicit  Reply With Quote

30. ## Re: need 2 decimal places

Suggestion: while cleaning up the code, give your variables meaningful names. x, c, d, etc isn't very descriptive. Avoid naming textboxes with "txt" prefixes and also creating variables with "txt" prefixes. It makes it harder, when revisiting the code down the road, to know which is coming from user input and which are calculated variables.

Oh and while you're also at it, use Option Explicit
Yeah, I kinda stated that back in 25.  Reply With Quote

31. ## Re: need 2 decimal places Originally Posted by Electric Chuck The txti is a variable not a text box but it is not dimensioned
I'm just going to second what LaVolpe said. Put Option Explicit at the very top of every module in your project. That's pretty much mandatory to be a respected VB6 programmer. You can tell the VB6 IDE to always do that for you over in the settings (IDE menu --> Tools ---> Options): Doing this might solve many of your problems.  Reply With Quote

32. ## Re: need 2 decimal places

not sure what is different between your vb6 and your settings with mine,
could be a conversion difference between your textbox into numeric that sees "." as "," while for me it will cause an error and will treat . as ., not sure. only expert on this matter could answer.
no matter, this tells how sensitive this is, could work for u but not for somebody else, that is why u need to handle it with care, and that is why I use Val, this to be sure it will not crash.
I get mismatch. maybe thats what Dilettante thought that it will work using CDbl, and surely it does for him, but not for me in all cases.
if not use Val he need to have a function that will determine that the value in the textbox can be converted into CDbl and all that trouble is not worth it when u can use Val()  Reply With Quote

33. ## Re: need 2 decimal places Originally Posted by SamOscarBrown @baka....don't know what you're doing differently, but when I comment out the line that 'sets' txthm to 11.5, and then type "11.5" into the textbox and run the command button, all three debugs print "25.875" (w/o the quotes)
My guess would be different locale settings. In the US I would expect that 11,5 to be an issue and 11.5 to work just fine.  Reply With Quote

34. ## Re: need 2 decimal places

@baka....probably IS locale settings...here in the US when I ran it with 11.5 TYPED INTO txthm, I get

25.875
25.875
25.875

when I type in 11,5 (that's a comma in there), I get

258.75
24.75
258.75

Now, THAT is one to figure out, but you DO see the issue with Val()?  Reply With Quote

35. ## Re: need 2 decimal places

yeah, so, the locale will also be an issue, in the 2nd test, all 3 values are wrong.
that means, we need to have a function that will replace , to .
and using 11.5, it will only work using Val() for me.

otherwise u need to have a locale aware function.

we could of course do the reverse, change . to ,
doing so, we dont use Val. (that works for me, but not for you)

no matter what we are doing, we need to either use "." or "," and replace either with the one we want to use.
another thing is consistency, floating point is using "." for the decimals, while strings, you are using "," its quite confusing.
that is why Im more used to "." and Val() is converting it correctly.

conclusion:
- Val is working for both of us using "."
- Val is working for me, if "." but not for ","
- I get error if I try to use "." without Val
- For you "," will result in wrong numbers in all cases
- For you "." will work for all cases.  Reply With Quote

36. ## Re: need 2 decimal places

I have cleaned up the program and dimensioned everything as Double.
the code will not put 11.25 in Txttotcubic.text textbox I get 11. (Eleven and period)
I tried Val and Cdbl no luck

it's simple addition 4.5 + 4 +2.25 + 0.5 = 11.25

If Combo6.Text = "12" Then
Txte = 2.25 * Txthm.Text
Txtsize(7).Text = Txte
'Txttotcubic.Text = 2.25 * (X + b + c + d) + (Txtsize(7).Text) + txtf + txtG + txth + txti
'Txttotcubic.Text = 2.25 * (0 + 0 + 0 + d) + (4.5) + 4 + 0 + 2.25 + 0.5
'Sum = CDbl((2.25 * (X + b + c + d)) + CDbl(Txte) + CDbl(Txtf) + CDbl(txtG) + CDbl(Txth) + CDbl(Txti))
Sum = (Val(Txte) + Val(Txtf) + Val(Txth) + Val(Txti))
Txttotcubic.Text = Sum
End If

I'm at a loss  Reply With Quote

37. ## Re: need 2 decimal places

Comment out all the calculation code and try this single line of code:

Code:
`Txttocubic.Text = 11.25`
What happens?  Reply With Quote

38. ## Re: need 2 decimal places

If it shows up as just 11 (as I suspect it will), then I am going to guess that you have the "MaxLength" property of the Txttocubic textbox set to 2, and it should be set to 0.  Reply With Quote

39. ## Re: need 2 decimal places

First thing first. What value do Txthm.Text contains?

try this:

Code:
```Dim Txte as Double

Txte = 2.25 * Val(Txthm.Text)
Txte = 2.25 * CDbl(Txthm.Text)
Txte = 2.25 * Txthm.Text```
what is the result of Txte in each one? are there any differences between the 3?
and what is the expected value of Txte.  Reply With Quote

40. ## Re: need 2 decimal places Originally Posted by Electric Chuck I'm at a loss
You could try attaching the project so we can see what is going on.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•