Format the amount in JQuery

Source: Internet
Author: User
: This article mainly describes how to format the amount in JQuery. For more information about PHP tutorials, see.
/*** Round the value (retain 2 decimal places) and format it into the amount format ** @ param num value (Number or String) * @ return value format String, for example, '200' * @ type String */function formatCurrency (num) {num = num. toString (). replace (/\ $ | \,/g, ''); if (isNaN (num) num =" 0 "; sign = (num = Math. abs (num); num = Math. floor (num * 100 + 0.50000000001); cents = num % 100; num = Math. floor (num/100 ). toString (); if (cents <10) cents = "0" + cents; for (var I = 0; I <Math. floor (num. length-(1 + I)/3); I ++) num = num. substring (0, num. length-(4 * I + 3) + ',' + num. substring (num. length-(4 * I + 3); return (sign )? '': '-') + Num + '. '+ cents);}/*** round the value (retain 1 decimal place) and format it into the amount form ** @ param num value (Number or String) * @ return: a String in the amount format, for example, '000000' * @ type String */function formatCurrencyTenThou (num) {num = num. toString (). replace (/\ $ | \,/g, ''); if (isNaN (num) num =" 0 "; sign = (num = Math. abs (num); num = Math. floor (num * 10 + 0.50000000001); cents = num % 10; num = Math. floor (num/10 ). toString (); for (var I = 0; I <Math. floor (num. length-(1 + I)/3); I ++) num = num. substring (0, num. length-(4 * I + 3) + ',' + num. substring (num. length-(4 * I + 3); return (sign )? '': '-') + Num + '. '+ cents);} // add the amount to format jQuery. extend ({formatFloat: function (src, pos) {var num = parseFloat (src ). toFixed (pos); num = num. toString (). replace (/\ $ | \,/g, ''); if (isNaN (num) num =" 0 "; sign = (num = Math. abs (num); num = Math. floor (num * 100 + 0.50000000001); cents = num % 100; num = Math. floor (num/100 ). toString (); if (cents <10) cents = "0" + cents; for (var I = 0; I <Math. floor (( Num. length-(1 + I)/3); I ++) num = num. substring (0, num. length-(4 * I + 3) + ',' + num. substring (num. length-(4 * I + 3); return (sign )? '': '-') + Num + '.' + cents );}});

The above introduces the formatting of the amount in JQuery, including the content, and hopes to help friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.