Js jquery version of the thousands of bit Conversion Function (non-regular, extremely efficient)

Source: Internet
Author: User

Js jquery version of the thousands of bit Conversion Function (non-regular, extremely efficient)

I didn't expect js to have no processing functions formatted in thousands of BITs (for example, 1,234.01). I searched the internet and processed them in regular expressions. The efficiency of regular expressions is not flattering, and the resource consumption speed is slow (although the processing will be more intuitive ).

Therefore, a pure numeric processing function is specially written to process the final output string with thousands of sub-bits and encapsulated into a jQuery function package. The processing efficiency is very high and can be used frequently and directly on the code. You can also click to download the min compression version.

If you are not using the jQuery environment, you can use the source code and re-encapsulate it into your own functions.

 

 

 

 

/*** The amount is separated by a comma * @ character_set UTF-8 * @ author Jerry. li (hzjerry@gmail.com) * @ version 1.4.04.08.24.2143 * Example * *      alert($.formatMoney(1234.345, 2)); //=>1,234.35 *      alert($.formatMoney(-1234.345, 2)); //=>-1,234.35 *      alert($.unformatMoney(1,234.345)); //=>1234.35 *      alert($.unformatMoney(-1,234.345)); //=>-1234.35 * */; (Function ($) {$. extend ({/*** number shard formatting * @ public * @ param mixed mVal value * @ param int iAccuracy decimal precision (2 by default) * @ return string */formatMoney: function (mVal, iAccuracy) {var fTmp = 0.00; // temporary variable var iFra = 0; // var iInt = 0 in the fractional part; // integer part var aBuf = new Array (); // output cache var bPositive = true; // Save the positive and negative values mark (true: Positive) /*** output a fixed-length string, which is not enough to fill in 0 *
  • Closure Functions
  • * @ Param int iVal value * @ param int iLen output length */function funZero (iVal, iLen) {var sTmp = iVal. toString (); var sBuf = new Array (); for (var I = 0, iLoop = iLen-sTmp.length; I = 0); // obtain the positive and negative signs fTmp = (isNaN (fTmp = parseFloat (mVal )))? 0: Math. abs (fTmp); // force convert to absolute value number floating point // all content is processed using positive number rules iInt = parseInt (fTmp ); // separate integer part iFra = parseInt (fTmp-iInt) * Math. pow (10, iAccuracy) + 0.5); // separate the fractional part (rounding) do {aBuf. unshift (funZero (iInt % 1000, 3);} while (iInt = parseInt (iInt/1000); aBuf [0] = parseInt (aBuf [0]). toString (); // remove the leading 0 return (bPositive )? '': '-') + ABuf. join (',') + '.' + (0 = iFra )? '00': funZero (iFra, iAccuracy ));}, /*** convert a numeric string in the kilobytes format to a floating point number * @ public * @ param string sVal numeric string * @ return float */unformatMoney: function (sVal) {var fTmp = parseFloat (sVal. replace (/,/g, ''); return (isNaN (fTmp )? 0: fTmp) ;},}) ;}( jQuery );

     

Related Article

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.