Page 1 of 2 12 LastLast
Results 1 to 40 of 59

Thread: need 2 decimal places

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    Arrow 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 ?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    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.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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).

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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.
    Last edited by baka; Sep 22nd, 2020 at 12:03 AM.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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
    Sam I am (as well as Confused at times).

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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.
    Sam I am (as well as Confused at times).

  10. #10
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: need 2 decimal places

    Quote Originally Posted by dilettante View Post
    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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    Arrow 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)

    Reply to all
    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

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Last edited by LaVolpe; Sep 22nd, 2020 at 09:19 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: need 2 decimal places

    Quote Originally Posted by SamOscarBrown View Post
    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.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    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

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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.
    Sam I am (as well as Confused at times).

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    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
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  18. #18
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    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?

  20. #20
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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
    Sam I am (as well as Confused at times).

  21. #21
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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)
    Sam I am (as well as Confused at times).

  22. #22
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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
    Last edited by baka; Sep 22nd, 2020 at 10:23 AM.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    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

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    Re: need 2 decimal places

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

  25. #25
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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.
    Sam I am (as well as Confused at times).

  26. #26
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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)
    Sam I am (as well as Confused at times).

  27. #27
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: need 2 decimal places

    Quote Originally Posted by Electric Chuck View Post
    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.

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    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.
    Again your help is appreciated.

    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

  29. #29
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  30. #30
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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.
    Sam I am (as well as Confused at times).

  31. #31
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: need 2 decimal places

    Quote Originally Posted by Electric Chuck View Post
    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):

    Name:  OptionExplicit.jpg
Views: 236
Size:  39.8 KB

    Doing this might solve many of your problems.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  32. #32
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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()

  33. #33
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: need 2 decimal places

    Quote Originally Posted by SamOscarBrown View Post
    @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.

  34. #34
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    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

    respectively (your code)

    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()?
    Sam I am (as well as Confused at times).

  35. #35
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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.
    Last edited by baka; Sep 22nd, 2020 at 04:03 PM.

  36. #36

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    21

    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

  37. #37
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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?

  38. #38
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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.

  39. #39
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    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.

  40. #40
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: need 2 decimal places

    Quote Originally Posted by Electric Chuck View Post
    I'm at a loss
    You could try attaching the project so we can see what is going on.

Page 1 of 2 12 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width