JS価格のフォーマット番号と金額

この方法の一つ:

    = ABSの機能(ヴァル){
 // 変換点の量- >要素小数点以下2桁および3つの毎カンマ1,234.56分離
      するvar STR =(ヴァル/ 100).toFixed(2)+ '';
       VARの intSum = STR。 ( "" 0、str.indexOfは())ストリングを交換(/ \ B(=(:。?? \ {D} 3)+ $)/ G ''); // 取得整数部
      VaRの DOT str.substring =(str.length、str.indexOf() "")// 検索の小数部取る
      のvar RET = intSum + DOTを、
       リターンRET; 
    } 
    はconsole.log(ABS( 2,000,000)); // 出力- > 20,000.00

方法2:

関数をnumber_format(数、小数、dec_point、thousands_sep){
     / * 
    *パラメータ:
    *番号:フォーマットする数
    *小数:予約小数点
    * dec_point:10進記号
    * thousands_sep:千記号
    * * /  = (番号+ '').replace(/ [^ 0-9 -ee +。] / G '' );
     VARの N-isFiniteの方=(数+)0:!?+ 数、
        PREC!= isFiniteの方(+小数) ?0 :Math.abs(小数)、
        9月には、 =(typeof演算 thousands_sep === '不定') ' '?:thousands_sep、
        12月 =(typeof演算 dec_point === '不定')?'。':dec_point、
        S = '' 
        toFixedFix = 関数(N、PREC){
             VARの K = Math.pow(10 、PREC)。
            戻り '' + Math.ceil(N * K)/ K。
        }。
 
    S =(PREC toFixedFix(N、PREC): '' N +恐らくMath.round()スプリット。?)( '' );
    VAR再= /( - \ D +?)(\ dの{3})/ 一方、(re.test(S [0 ])){ 
        S [ 0] =のS [0] .replace(RE、 "$ 1" + 9月+ "$ 2" )。
    } 
 
    もし、((S [1] || '').LENGTH < PREC){ 
        P [ 1] = P [1] || 「」 ;
        S [ 1] + = 新しいアレイ(PREC - S [1] .LENGTH + 1).join( '0' )。
    } 
    戻りs.join(分解)。
}

使用します。

 VaRの NUM =をnumber_format(1234567.089、2、、 " ""。")。// 1,234,567.09

丸めに対する一つのより直接的なアプローチ:

関数をnumber_format(数、小数、dec_point、thousands_sep){
         / * 
        *パラメータ:
        *番号:フォーマットする数
        *小数:予約小数点
        * dec_point:10進記号
        * thousands_sep:千記号
        * * /  = (番号+ '').replace(/ [^ 0-9 -ee +。] / G '' );
         VARの N-isFiniteの方=(数+)0:!?+ 数、
 
            PREC!= isFiniteの方(+小数) ?0 :Math.abs(小数点以下)、
            9月 =(typeof演算 thousands_sep === '未定義') ''?:thousands_sep、
            12月 =(typeof演算dec_point === '未定義')?'' :dec_point、
            S = '' 
            toFixedFix = 関数(N、PREC){
                 VARの K = Math.pow(10 、PREC)。
                戻り '' + Math.floor(N * K)/ K。
            }。
        S =(PREC toFixedFix(N、PREC): '' N + Math.floor()スプリット。?)( '' );
        VAR再= /( - \ D +?)(\ dの{3})/ 
        console.log(S) 
        一方(re.test(S [0 ])){ 
            S [ 0] =のS [0] .replace(RE、 "$ 1" + 9月+ "$ 2" )。
 
        ((S [1] || '').LENGTH < PREC){ 
            S [ 1] = sの[1] || '' ; 
            S [ 1] + = 新しいアレイ(PREC - S [1] .LENGTH + 1).join( '0' )。
        } 
        戻りs.join(分解)。
    } 
    をnumber_format( 1234567.089、2、、 " ""。")。// 1,234,567.08

 

おすすめ

転載: www.cnblogs.com/lguow/p/10955580.html