How to add '$' symbol and a thousand seperator ',' to numbers in text field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cptuser
    New Member
    • Mar 2007
    • 30

    How to add '$' symbol and a thousand seperator ',' to numbers in text field

    Hi,

    I'm a novice at this. I've managed to learn by example and come up with this Javascript code from various examples on the web.

    Could someone please help me with providing code that will display the $ sign at the beginning of the number and seperate the number at thousands by a ',' in the text field (price).

    I have no idea how to do this. Many thanks

    [CODE=javascript]
    function aircraftcost()
    {
    var
    buy_cost = document.getEle mentById('buypr ice').value,
    sale_price = 500 * 30 * buy_cost; //is it correct to have a ; after buy_cost?

    document.getEle mentById('price ').value = (sale_price).to Fixed(2);
    //how do I add a '$' sign for this 'price' value and insert a ',' at the thousands mark?
    }
    [/CODE]
  • waltapp
    New Member
    • Nov 2008
    • 31

    #2
    The dollar sign is easy. When you do your document.write go
    document.write( '$'+variable)

    Putting in commas however is a little more difficult and can be done in a variety of ways.

    Comment

    • waltapp
      New Member
      • Nov 2008
      • 31

      #3
      I thought about the comma and I think the easiest way is to take your first variable as a number and divide by 1000 and use the math.round method to round the number . Now you can multiply by 1000 and subtract it from your original number. Now you have a number that you can use the toFixed(2) to create your text string. Now you can use the document.write statement to finish.

      document.write( '$'+variable+', '+secondString)

      Unless you are running into millions. This is the easiest.

      Comment

      • cptuser
        New Member
        • Mar 2007
        • 30

        #4
        Thanks for the reply. I don't see anywhere in my code where document.write is., but in anycase I've managed to derive from your suggestion and used '$'+ at the beginning of the variable - so thank you, that worked fine
        [CODE=javascript]
        document.getEle mentById('price ').value = '$+((sale_price ).toFixed(2));
        [/CODE]
        With regards to the thousands seperator, are you able to give me examples of how this can be done?
        Any one else able to provide a simple, clean thousand seperator?

        Many thanks.

        Comment

        • waltapp
          New Member
          • Nov 2008
          • 31

          #5
          I was writing the comma reply when you posted.

          Comment

          • cptuser
            New Member
            • Mar 2007
            • 30

            #6
            Originally posted by waltapp
            I thought about the comma and I think the easiest way is to take your first variable as a number and divide by 1000 and use the math.round method to round the number . Now you can multiply by 1000 and subtract it from your original number. Now you have a number that you can use the toFixed(2) to create your text string. Now you can use the document.write statement to finish.

            document.write( '$'+variable+', '+secondString)

            Unless you are running into millions. This is the easiest.

            Are you able to provide assistance taking into consideration my code that I've included in my original post. I'm struggling to follow. I dont seem to have document.write anywhere (not sure what that does and how it would fit in the code).

            Comment

            • waltapp
              New Member
              • Nov 2008
              • 31

              #7
              your variable is sale_price

              sale_price*1000
              do not apply the toFixed yet as it turns the number into a text string.

              apply the Math.round method after dividing by 1000.
              This rounds your number off as an integer.
              If your number was 10000.55 it now becomes 10. so I would go
              var newnumber=sale_ price/1000
              newnumber=Math. round(newnumber )
              newnumber=newnu mber*1000
              sale_price=sale _price-newnumber
              (sale_price).to Fixed(2)

              then however you are dealing with the result= '$'+newnumber+' ,'+sale_price

              Regards

              Comment

              • cptuser
                New Member
                • Mar 2007
                • 30

                #8
                Originally posted by waltapp
                your variable is sale_price

                sale_price*1000
                do not apply the toFixed yet as it turns the number into a text string.

                apply the Math.round method after dividing by 1000.
                This rounds your number off as an integer.
                If your number was 10000.55 it now becomes 10. so I would go
                var newnumber=sale_ price/1000
                newnumber=Math. round(newnumber )
                newnumber=newnu mber*1000
                sale_price=sale _price-newnumber
                (sale_price).to Fixed(2)

                then however you are dealing with the result= '$'+newnumber+' ,'+sale_price

                Regards
                Thanks for that. I tried your suggestion, yet it's not really working. What I need is to say have a number like 34000 to be displayed as 34,000, or 5000 as 5,000 or 300 to stay as 300.

                I'm not sure if I had made it clear in my original post.
                Many thanks.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  You may find this page useful.

                  Comment

                  Working...