Web Analytics Made Easy -
StatCounter 2 decimals and adding , for 1,000+ - CodingForum

Announcement

Collapse
No announcement yet.

2 decimals and adding , for 1,000+

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2 decimals and adding , for 1,000+

    i'm stuck trying to have all numbers shown with only 2 decimals and adding the , for numbers 1,000 plus. how can i do this?

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function roundoff(amount) {
    return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
    }


    function total(what,number) {
    var grandTotal = 0;
    var yearTotal = 0;
    for (var i=0;i<number;i++) {
    if (what.elements['price' + i].value == '')
    what.elements['price' + i].value = '0.00'; // fix for Opera.

    what.elements['subtotal' + i].value=(what.elements['quantity' + i].value - 0) * (what.elements['price' + i].value - 0);
    if (what.elements['quantity' + i].value == "0")
    what.elements['subtotal' + i].value = "0.00";

    subtotal=what.elements['subtotal' + i].value
    grandTotal += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0);
    }

    subtotal=roundoff(Math.round(subtotal*Math.pow(10,2))/Math.pow(10,2));
    what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(10,2))/Math.pow(10,2));

    what.elements['yearTotal'].value=(what.elements['grandTotal'].value - 0) * (12);

    }
    //-->
    </SCRIPT>

  • #2
    I did a currency function for this

    The only way I know is to manually parse the numbers yourself...

    :\

    -S. Bob

    Comment


    • #3
      Number.prototype.prettyPrint = function() {
      return this.toFixed(2).split('').reverse().join('').replace(/(\d{3}(?=\d))/g,'$1,').split('').reverse().join('');
      }

      Works in IE5.5+ and NS6 at least.

      Heh, gotta love the one-liners I pull outta nowhere with some crafty RegExp work.

      BTW, to use it:


      3547654.5966.prettyPrint() == "3,547,654.60"
      Last edited by jkd; Jul 13, 2002, 01:16 AM.
      jasonkarldavis.com

      Comment


      • #4
        check this out - how can i add this to my form???

        Large Collection of JavaScript source code. Choose from thousands of free scripts. JavaScript tutorials with example code. Excellent reference material for JavaScript. If you need help with JavaScript. JavaScript Made Easy is the place to find it.


        is there ANY way to add this so it does it automatically with what is entered on my form (without entering a number and clicking the convert button)???

        Comment


        • #5
          Use the onblur event handler of the input element.

          Hope that helps!

          Happy coding!

          Comment


          • #6
            i've been pulling my hair out trying to get this to work - can anyone tell me what i need to put and where (talk to me like i'm 4!!)???

            Comment


            • #7
              onblur="this.value = parseFloat(this.value).prettyPrint()"

              In an <input type="text"/> should do the trick.
              jasonkarldavis.com

              Comment

              Working...
              X