Working with numbers

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

    #1

    Working with numbers

    Anyone know of a quick method to make numbers input from a textbox work with
    math. For example, if I have a text box where people might enter a value
    like:

    $567,000.23

    I want to work with only 567000.23

    Is there an easy way to do that? Thanks, Ike


  • Andrew Urquhart

    #2
    Re: Working with numbers

    *Ike* wrote:[color=blue]
    > Anyone know of a quick method to make numbers input from a textbox
    > work with math. For example, if I have a text box where people might
    > enter a value like:
    >
    > $567,000.23
    >
    > I want to work with only 567000.23
    >
    > Is there an easy way to do that? Thanks, Ike[/color]

    To reduce the degree of variation in the input data I'd provide the user
    with unambiguous input boxes where the currency unit can be selected in
    isolation, the integer money units are then input in one box and the
    small change goes in another box. That way the user doesn't have to make
    any decisions as to what the currency format should be since there is no
    choice! Any extraneous characters in the input can then be queried more
    successfully.

    That said, if you don't want to do that then the following is a starter:

    var strInput = "$567,000.2 3";
    var dblInput = strInput.replac e(/[^\d\.]/g, "");

    You can then check isNaN(dblInput) . However, the above won't work for
    the large number of countries that interchange . and , delimiters in
    numbers - e.g. the above won't work for: Euro567.000,23
    --
    Andrew Urquhart
    - FAQ: www.jibbering.com/faq/
    - Archive: www.google.com/groups?q=comp.lang.javascript
    - My reply address is invalid, see www.andrewu.co.uk/contact/


    Comment

    • Mick White

      #3
      Re: Working with numbers

      Ike wrote:
      [color=blue]
      > Anyone know of a quick method to make numbers input from a textbox work with
      > math. For example, if I have a text box where people might enter a value
      > like:
      >
      > $567,000.23
      >
      > I want to work with only 567000.23
      >
      > Is there an easy way to do that? Thanks, Ike
      >
      >[/color]

      value= + (value.replace(/[$,]/g,""))
      if(!isNaN(value )){
      // It's a number, use number in calculations
      }

      Place any unwanted character into the bracket.

      [£¢ $¥,€]

      Pound sterling, US cent, space, US dollar, yen, euro.


      Mick

      Comment

      • Dr John Stockton

        #4
        Re: Working with numbers

        JRS: In article <KIdEc.245$dm1. 135@newsfe6-gui.server.ntli .net>, seen
        in news:comp.lang. javascript, Andrew Urquhart <useWebsiteInSi gnatureToRe
        ply@spam.invalid> posted at Tue, 29 Jun 2004 12:59:08 :[color=blue]
        >
        >You can then check isNaN(dblInput) . However, the above won't work for
        >the large number of countries that interchange . and , delimiters in
        >numbers - e.g. the above won't work for: Euro567.000,23[/color]

        The browser's decimal separator is
        Char = 1.5.toString(). charAt(1) // tested only in UK IE4
        // (3/2) might be safer

        I don't know of anywhere offhand that uses a character other than the
        (keyboard) . or , characters, ASCII 46 and 44. Anyone?

        But there may well be one or more other Unicode decimal separators; and,
        if so, I know of one person who, it must be supposed, would use nothing
        but one of those.

        In addition to stripping the expected, or any, unwanted characters, one
        can if necessary rectify the decimal separator before conversion to
        Number.

        Actually, one should not strip the currency indication; if it can be
        ignored, it should be forbidden, and otherwise it needs to be
        considered.

        In some contests, it may be necessary to consider alien formats for
        negative numbers.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
        Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links;
        some Astro stuff via astro.htm, gravity0.htm; quotes.htm; pascal.htm; &c, &c.
        No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

        Comment

        Working...