2017-08-25 97 views
0

我有這個功能,這是我從其他線程把這裏stacksoverflow讓我的號碼從1000正確的格式,以1000 但是這個代碼刪除小數如何在jquery中包含小數number_format();

function number_format(number, decimals, dec_point, thousands_sep) { 
    // Strip all characters but numerical ones. 
    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.round(n * k)/k; 
    }; 

    // Fix for IE parseFloat(0.55).toFixed(0) = 0; 
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.'); 

    if (s[0].length > 3) { 
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); 
    } 

    if ((s[1] || '').length < prec) { 
    s[1] = s[1] || ''; 
    s[1] += new Array(prec - s[1].length + 1).join('0'); 
    } 

    return s.join(dec); 
} 

我想這種格式1,000.76,但我不知道如何修改它TYIA

+0

您可以節省一大堆的代碼,只需使用['的toLocaleString()'](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString),而不是... –

回答

0

而是大的功能,你可以使用REGEX獲得逗號和小數點:

function digits(str){ 
 
    str += ''; 
 
    return str.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); 
 
} 
 

 
console.log(digits(10008.20)); 
 
console.log(digits(1000.76));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

我用這個函數來驗證我項目中的貨幣和金額,如果你願意的話可以試試看。該函數可與此情況下:

  • 1200(簡單數=> 1200.00)
  • 1,123.45(美國格式貨幣)
  • 1200或-1200(帶符號數字)
  • 124.5(小數點後=> 124.50)
  • 0.45(十進制)
  • 0.45(十進制無零=> 0.45)
  • +.1(符號和小數點無零=> 0.10)

而且您可以選擇要驗證的小數位數。我使用兩個,但你可以改變它。

// change this to test 
 
var amount = "1000.76"; 
 

 
function validateAmount(amount){ 
 
\t \t amount = amount.trim(); 
 
    
 
\t \t var result = "", comma =",", dot="."; 
 
    
 
    // check if amount is in american currency format, comma as thousand separator and dot and decimal 
 
    var usRegExp = new RegExp("^(\-|[+]?)(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?((\.)?|(\.?[0-9]{1,2})?)$") 
 

 
    // test if amount is a number or is formatted as currency 
 
    if(usRegExp.test(amount)) 
 
    { 
 
     var decimalSep = dot, thousandSep = comma 
 

 
     var vals = amount.split(decimalSep) 
 

 
     // remove thousand separator 
 
     vals[0] = vals[0].replace(thousandSep, '') 
 

 
     // add thousand separator to floor part of the number 
 
     var integer = vals[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandSep) 
 

 
     // if amount start with comma or dot, trasform to decimal 
 
     if(typeof integer == 'undefined' || integer == '') 
 
     integer = '0'+ integer 
 

 
     // if amount is only + or - add zero after the sign \t 
 
     if(integer == '-' || integer == '+') \t 
 
     integer += '0' 
 

 
     if(amount.indexOf(decimalSep) != -1) 
 
     { 
 
     decimal = vals[1] 
 

 
     // amount has decimals 
 
     if(decimal != 'undefined' && decimal != '') 
 
     { 
 
      if(decimal.length == 2) 
 
      { 
 
      result = integer + dot + decimal; 
 
      } 
 

 
      // if has more than two decimals throw error 
 
      if(decimal.length > 2) 
 
      { 
 
      // here you can check decimal lenght and throw error or manage it 
 
      console.log("more than two decimal") 
 
      } 
 
      // only one decimal add zero 
 
      else if(decimal.length == 1) 
 
      { 
 
      result = integer + dot + decimal + '0'; 
 
      } 
 
     } 
 
     // amount end with comma 
 
     else 
 
      result = integer + dot + '00'; 
 

 
     } 
 
     // simple amount without decimal, add dot and decimal part 
 
     else 
 
     { 
 
     result = integer + dot + '00' 
 
     } 
 
    } 
 
    else 
 
    { \t 
 
     // throw error is not a value number 
 
     console.log("error") 
 
    } 
 
    
 
    return result; 
 
} 
 

 
$('#id').text(validateAmount(amount))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="id" > 
 

 
</div>