JS implementation formatted digital and amount function simple example

This example tells the formatted number and amount functionality of JS implementation. Share to everyone for reference, specifically:

Formatted amount:

  function number_format (Number, Decimals, DEC_POINT, THOUSANDS_SEP) {/ * * Parameter Description: * Number: To format the number * Decimals: Keep a few decimal * Dec_Point: decimal point symbol * thousands_sep: omit symbol * * / Number = (Number + ') ) .replace (/ [^ 0-9 + -ee.] / g, ''); var n =! isfinite (+ number)? 0: + number, prec =! isfinite (+ DECIMALS)? 0: Math. ABS (Decimals), SEP = (TypeOf Thousands_sep === 'undefined')? ',': thousands_sep, dec = (TypeOf Dec_Point === 'undefined')? '.': Dec_point, s = ', Tofixedfix = Function (N, PREC) {var k = math.pow (10, prec); return '' + math.ceil (n * k) / k;}; s = (PREC? Tofixedfix (n, prec): '' + Math.Round (n)). Split ('.'); Var RE = / (-? \ D +) (\ d {3}) /;While (RE.TEST (S [0])) {s [0] = s [0] .replace (RE, "$ 1" + SEP + "$ 2");} IF ((S [1] || '' How to use: < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}

 

VAR NUM = Number_Format (1234567.089, 2, “.”, ” , // 1, 234, 567.09console.log (NUM);

Again, directly go to the method:
   
Function Number_Format (Number, Decimals, Dec_Point, Thousands_sep) {/ * * Parameter Description: * Number: Number To format * Decimals: Keep a few decimal * Dec_Point: decimal point symbol * thousands_sep: Thousandificificown symbol * * / number = (Number + ”) .replace (/ [^ 0-9 + -ee.] / G, ”); var n =! Isfinite (+ Number)? 0: + Number , prec =! isfinite (+ decimals)? 0: Math.Abs ​​(Decimals), SEP = (TypeOf Thousands_sep === ‘undefined’)? ‘,’: thousands_sep, dec = (TypeOf Dec_Point === ‘undefined’) ?’.’: Dec_point, s = ‘, TofixedFix = function (n, prec) {var k = math.pow (10, prec); return’ ‘+ math.floor (n * k) / k;}; = (PREC? TOFIXEDFIX (N, PREC): ” + math.floor (n)). Split (‘.’); var RE = / (-? \ d +) (\ d {3}) /; console. Log (s) While (RE.TEST (s [0])) {s [0] = s [0] .replace (RE, “$ 1” + SEP + “$ 2″);} if (((S [1] || ”) .length

Interested friends can use

online HTML / CSS / JavaScript code running tool
http: //tools.jb51 .NET / CODE / HTMLJSRUN Test the above code operation results.
  PS: Here you recommend several computing tools for the further reference reference: < prec) {
      s[1] = s[1] || '';
      s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
var num=number_format(1234567.089, 2, ".", ",");//1,234,567.08
console.log(num)

 
Online 1 yuan function (equation) solving calculation tool:

http://tools.jb51.net/jisuanqi/equ_jisuanqi

Scientific calculator online usage _ Advanced calculator online calculation: http://tools.jb51.net/jisuanqi/jsqkexue

Online calculator _ standard calculator:
http: //tools.jb51. Net / jisUNQI / JSQ

More about JavaScript related content, readers who are interested in this site: “JavaScript Mathematical Computing Summary”, “JavaScript Data Structure and Algorithm Skills Summary”, “JavaScript Action Skills Summary” “JavaScript Sort Algorithm Summary”, Summary of JavaScript Overall Algorithm and Skills, Summary, “JavaScript Finding Algorithm Skills Summary” and “JavaScript Error and Debugging Skills Summary”

I hope this article will be designed for the JavaScript programming. help.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment