Formatted Number problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Roys

    Formatted Number problem

    Hi

    I have a textbox which displays a formatted numeric. eg: 100,000
    When I retreive this value for calculations, it is valued at 100
    eg:
    calc = val(txtbox) * 2
    calc will equal 200, not 200,000.

    Why is this, how do I fix it ??



  • Stephane Richard

    #2
    Re: Formatted Number problem

    Hi Roy,

    The reason is simple,.the isn't part of a valid pure integer numeric value.
    Therefore when doing a val of the textbox's value it truncates it to the ,
    so you get 100 in this case.


    If the textbox absolutely needs to display the number formatted as such,
    what you need to do is use the CDbl function when retreiving the value of
    the formatted textbox.

    Dim Value As Integer

    Value = CDbl(TextBox1.T ext) ' will give you 100000 instead of 100



    --
    Stéphane Richard
    "Ada World" Webmaster



    "The Roys" <trevr62@hotmai l.com> wrote in message
    news:uWNmb.1660 09$bo1.75947@ne ws-server.bigpond. net.au...[color=blue]
    > Hi
    >
    > I have a textbox which displays a formatted numeric. eg: 100,000
    > When I retreive this value for calculations, it is valued at 100
    > eg:
    > calc = val(txtbox) * 2
    > calc will equal 200, not 200,000.
    >
    > Why is this, how do I fix it ??
    >
    >
    >[/color]


    Comment

    • J French

      #3
      Re: Formatted Number problem

      On Sun, 26 Oct 2003 11:08:10 GMT, "The Roys" <trevr62@hotmai l.com>
      wrote:
      [color=blue]
      >Hi
      >
      >I have a textbox which displays a formatted numeric. eg: 100,000
      >When I retreive this value for calculations, it is valued at 100
      >eg:
      >calc = val(txtbox) * 2
      >calc will equal 200, not 200,000.
      >
      >Why is this, how do I fix it ??[/color]

      Many ways of fixing it
      However the solution depends on how the ',' got into the string in the
      first place

      Comment

      • Randy Birch

        #4
        Re: Formatted Number problem

        Did you try the code you posted? Although correct regarding Val(),
        obviously, since the result exceeds the maximum value the integer data type
        can hold, value has to be declared as long at a minimum. In addition, it is
        unnecessary to use CDbl .. CLng does the trick as well.


        Private Sub Command1_Click( )

        Dim Value As Long

        Text1.Text = "100,000"

        Value = Text1.Text
        Print Value 'prints 100000

        Value = CLng(Text1.Text ) * 2
        Print Value 'prints 200000

        End Sub



        --

        Randy Birch
        MVP Visual Basic

        Please respond only to the newsgroups so all can benefit.


        "Stephane Richard" <stephane.richa [email protected]> wrote in message
        news:mdOmb.9690 $1C5.8590@nwrdn y02.gnilink.net ...
        : Hi Roy,
        :
        : The reason is simple,.the isn't part of a valid pure integer numeric
        value.
        : Therefore when doing a val of the textbox's value it truncates it to the ,
        : so you get 100 in this case.
        :
        :
        : If the textbox absolutely needs to display the number formatted as such,
        : what you need to do is use the CDbl function when retreiving the value of
        : the formatted textbox.
        :
        : Dim Value As Integer
        :
        : Value = CDbl(TextBox1.T ext) ' will give you 100000 instead of 100
        :
        :
        :
        : --
        : Stéphane Richard
        : "Ada World" Webmaster
        : http://www.adaworld.com
        :
        :
        : "The Roys" <trevr62@hotmai l.com> wrote in message
        : news:uWNmb.1660 09$bo1.75947@ne ws-server.bigpond. net.au...
        : > Hi
        : >
        : > I have a textbox which displays a formatted numeric. eg: 100,000
        : > When I retreive this value for calculations, it is valued at 100
        : > eg:
        : > calc = val(txtbox) * 2
        : > calc will equal 200, not 200,000.
        : >
        : > Why is this, how do I fix it ??
        : >
        : >
        : >
        :
        :


        Comment

        • Tom Rathbun

          #5
          Re: Formatted Number problem

          The problem here may be that your computer is configured in the European
          style which uses the comma the same way we use the decimal point in the US.
          Snoop around in the control panel and you will find a setting for this. I
          had a customer that had a similar problem with some code I was "SURE"
          worked.

          "The Roys" <trevr62@hotmai l.com> wrote in message
          news:uWNmb.1660 09$bo1.75947@ne ws-server.bigpond. net.au...[color=blue]
          > Hi
          >
          > I have a textbox which displays a formatted numeric. eg: 100,000
          > When I retreive this value for calculations, it is valued at 100
          > eg:
          > calc = val(txtbox) * 2
          > calc will equal 200, not 200,000.
          >
          > Why is this, how do I fix it ??
          >
          >
          >[/color]


          Comment

          Working...