Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript Multiple number WITH dollar sign and comma.

    I'm new to this group. Hello!

    I'm having some trouble.

    I have a variable that is coming in and it has a dollar sign and a comma in it.

    For example

    The variable is called tmp_price.

    So lets say the amount is $350,000, how do I multiply it by .96? Javascipt blows up with the $ sign

    So I have this simple code:
    <SCRIPT Language = JavaScript>
    B = 100
    D = (B * .96)
    document.write(D)
    </SCRIPT>

    Which will produce: 96

    But if I replace "100" with tmp_price and that tmp_price is $350,000 I get an error.

    So how to I do a simple equation to multiply .96 time this variable with a dollar and comma in it.

    Thanks

    JP
    Last edited by jpater74; 12-12-2007 at 07:56 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you can use regex:

    http://www.javascriptkit.com/jsref/regexp.shtml

    to extract only the price, unformated, and to check if it's valid.

    Example:

    price = '$110,210';
    price.replace(/\D/g,'');

    to work with price as '$10.50', you must first split this string in two parts'
    '$10' and '50' and join it after processing.

    best regards

  • Users who have thanked oesxyl for this post:

    jpater74 (01-05-2008)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    A decimal point is allwed in a number, you just need to use a regular expression to get rid of the dollar sign and commas. It may be easier to just strip out everything that is not numeric.

    Code:
    var num = '$500.04';
    num = num.replace(/[\D\.-]+/g,'')-0;
    alert(num);
    Oh and if you want to put the currency symbol and commas back later see http://javascript.about.com/library/blnumfmt.htm
    Last edited by felgall; 12-12-2007 at 08:29 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by felgall View Post
    A decimal point is allwed in a number, you just need to use a regular expression to get rid of the dollar sign and commas. It may be easier to just strip out everything that is not numeric.

    Code:
    var num = '$500.04';
    num = num.replace(/[\D\.]+/g,'')-0;
    alert(num);
    what if num = '$500' without '.'?

  • #5
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Awesome oesxyl, that worked great!


  •  

    Posting Permissions

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