JavaScript format Numbers-Go

Source: Internet
Author: User

1 functionNumber_format (number, decimals, Dec_point, Thousands_sep,roundtag) {2     /*3 * Parameter Description:4 * Number: Numbers to format5 * Decimals: Keep several decimals6 * dec_point: decimal symbol7 * Thousands_sep: thousand-bit symbol8 * Roundtag: Rounding parameters, default "Ceil" Fetch up, "floor" is taken down, "round" rounded9     * */TenNumber = (number + "). Replace (/[^0-9+-ee.) /g, "); OneRoundtag = Roundtag | | "Ceil";//" ceil", "Floor", "round" A     varn =!isfinite (+number)? 0: +Number , -Prec =!isfinite (+decimals)? 0: Math.Abs (decimals), -Sep = (typeofThousands_sep = = = ' undefined ')? ‘,‘: Thousands_sep, theDec = (typeofDec_point = = = ' undefined ')? ‘.‘: Dec_point, -s = ', -Tofixedfix =function(n, prec) { -   +             varK = Math.pow (10, prec); - Console.log (); +   A             return"+ parsefloat (Math[roundtag] (parsefloat ((n * k). toFixed (prec*2))). ToFixed (prec*2))/K; at         }; -s = (prec? tofixedfix (N, prec): ' + Math.Round (n)). Split ('. ')); -     varRe =/(-?\d+) (\d{3})/; -      while(Re.test (s[0])) { -S[0] = S[0].replace (Re, "$" + Sep + "$ $")); -     } in   -     if((S[1] | | "). Length <prec) { toS[1] = S[1] | | ‘‘; +S[1] + =NewArray (Prec-s[1].length + 1). Join (' 0 '); -     } the     returnS.join (DEC); * } $Console.log (Number_format (2, 2, ".", ","))//"2.00"Panax NotoginsengConsole.log (Number_format (3.7, 2, ".", ","))//"3.70" -Console.log (Number_format (3, 0, ".", ","))//"3" theConsole.log (Number_format (9.0312, 2, ".", ","))//"9.03" +Console.log (Number_format (9.00, 2, ".", ","))//"9.00" AConsole.log (Number_format (39.715001, 2, ".", ",", "floor"))//"39.71" theConsole.log (Number_format (9.7, 2, ".", ","))//"9.70" +Console.log (Number_format (39.7, 2, ".", ","))//"39.70" -Console.log (Number_format (9.70001, 2, ".", ","))//"9.71" $Console.log (Number_format (39.70001, 2, ".", ","))//"39.71" $Console.log (Number_format (9996.03, 2, ".", ","))//"9996.03" -Console.log (Number_format (1.797, 3, ".", ",", "floor"))//"1.797"

JavaScript format Numbers-Go

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.