Add an integer to the thousand symbol in JavaScript

Source: Internet
Author: User

If num is an integer, convert it to a string with a thousands symbol:

Number (num). toString (). Replace (/(\d) (? = (\d{3}) + (?! \d))/g, ' $ ' + ', ');

In addition, Http://www.cnblogs.com/yeminglong/p/5301295.html gives the conversion format of decimals, as follows:

1 /**2 *3 * @param num4 * @param Precision5 * @param separator6 * @returns {*}7 *=======================================================8 * FormatNumber (10000) = "Up"9 * FormatNumber (10000, 2) = "10,000.00"Ten * FormatNumber (10000.123456, 2) = "10,000.12" One * FormatNumber (10000.123456, 2, ') = "Ten 000.12" A * FormatNumber (. 123456, 2, ') = "0.12" - * FormatNumber (2, ") =" 56.00 " - * FormatNumber (0, ") =" " the * FormatNumber (' 56. ') = "The " - * FormatNumber (' 56.a ') =nan - *======================================================= - */ + functionformatnumber (num, precision, separator) { -     varparts; +     //determine whether a number A     if(!isnan (parsefloat (num)) &&isfinite (num)) { at       //Convert similar. 5, 5. Data into 0.5, 5, for data precision processing, and why -       //Do not write directly if in judgment (!isnan (num = parsefloat (num)) && isfinite (num)) -       //is because parsefloat has a strange precision problem, such as parsefloat (12312312.1234567119) -       //the value becomes 12312312.123456713. -num =Number (num); -       //processing the number of decimal places innum = (typeofPrecision!== ' undefined '?num.tofixed (precision): num). toString (); -       //separating the fractional and integer parts of a number toParts = Num.split ('. ')); +     //integer part plus [separator] delimited, borrowing a well-known regular expression -Parts[0] = parts[0].tostring (). Replace (/(\d) (? = (\d{3}) + (?! \d)/g, ' $ ' + (separator | | ‘,‘)); the  *       returnParts.join ('. ')); $ }Panax Notoginseng     returnNaN; -}

1 /**2 * Convert numbers to currency format3 * @param decimals4 * @param dec_point5 * @param thousands_sep6 * @returns {string}7 */8number.prototype.format=function(decimals, Dec_point, thousands_sep) {9 varnum = ( This+ ')Ten. replace (/[^0-9+\-ee.) /g, "); One varn =!isfinite (+num)? 0: +num, APrec =!isfinite (+decimals)? 0: Math.Abs (decimals), -Sep = (typeofThousands_sep = = = ' undefined ')? ‘,‘: Thousands_sep, -Dec = (typeofDec_point = = = ' undefined ')? ‘.‘: Dec_point, thes = ', -Tofixedfix =function(n, prec) { - varK = Math.pow (10, prec); - return' + (Math.Round (n * k)/k) + . toFixed (PREC); - }; + //Fix for IE parsefloat (0.55). toFixed (0) = 0; As = (prec? tofixedfix (N, prec): ' +Math.Round (n)) at. Split ('. ')); - if(S[0].length > 3) { -S[0] = S[0].replace (/\b (? = (?: \ D{3}) + (?! \d))/g, Sep); - } - if((S[1] | | ‘‘) -. length <prec) { inS[1] = S[1] | | ‘‘; -S[1] + =NewArray (prec-s[1].length + 1) to. Join (' 0 '); + } - returnS.join (DEC); the}

Add an integer to the thousand symbol in JavaScript

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.