2012-04-27 49 views
2

我想刪除「」後面的數字。然後我要格式化的數(16810900.211233)喜歡這個(16810900),但我不知道該怎麼做,這是我的HTML是這樣的:如何格式化數字使用jquery

<link rel="stylesheet" type="text/css" href="http://cdn.webrupee.com/font" /> 
<div class="main"> 
<p class="active">10200.00</p> 
<p class="in_active">16810900.211233</p> 
<p class="active">0</p> 
<p class="in_active">-</p> 
<p class="active">1278200.543554</p> 
<p class="in_active">-</p> 
<p class="active">-</p> 
<p class="in_active">9,890.656</p> 
<p class="active">10,200</p> 
<p class="in_active">16810900</p> 
<p class="active">0</p> 
<p class="in_active">-</p> 
<p class="active">1278200.09</p> 
<p class="in_active">-</p> 
<p class="active">-</p> 
<p class="in_active">9890.00</p> 
</div>​ 

我在這裏試圖jsfiddle

+1

一些基本的輪功能應該做。見例如[http://stackoverflow.com/questions/4098685/jquery-rounding-numbers-to-2-digits-after-comma][1]。 [1]:http://stackoverflow.com/questions/4098685/jquery-rounding-numbers-to-2-digits-after-comma的 – gustafbstrom 2012-04-27 12:58:05

+0

可能重複[我怎樣才能在格式化數字作爲貨幣JavaScript?](http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript) – JJJ 2012-04-27 13:01:16

回答

2

http://ntt.cc/2008/04/25/6-very-basic-but-very-useful-javascript-number-format-functions-for-web-developers.html

沒有點重新發明輪子受騙.. :)

/** 
* Formats the number according to the ‘format’ string; 
* adherses to the american number standard where a comma 
* is inserted after every 3 digits. 
* note: there should be only 1 contiguous number in the format, 
* where a number consists of digits, period, and commas 
*  any other characters can be wrapped around this number, including ‘$’, ‘%’, or text 
*  examples (123456.789): 
*   ‘0′ - (123456) show only digits, no precision 
*   ‘0.00′ - (123456.78) show only digits, 2 precision 
*   ‘0.0000′ - (123456.7890) show only digits, 4 precision 
*   ‘0,000′ - (123,456) show comma and digits, no precision 
*   ‘0,000.00′ - (123,456.78) show comma and digits, 2 precision 
*   ‘0,0.00′ - (123,456.78) shortcut method, show comma and digits, 2 precision 
* 
* @method format 
* @param format {string} the way you would like to format this text 
* @return {string} the formatted number 
* @public 
*/ 

Number.prototype.format = function(format) { 
    if (! isType(format, ’string’)) {return 」;} // sanity check 

    var hasComma = -1 < format.indexOf(’,'), 
    psplit = format.stripNonNumeric().split(’.'), 
    that = this; 

    // compute precision 
    if (1 < psplit.length) { 
    // fix number precision 
    that = that.toFixed(psplit[1].length); 
    } 
    // error: too many periods 
    else if (2 < psplit.length) { 
    throw(’NumberFormatException: invalid format, formats should have no more than 1 period: ‘ + format); 
    } 
    // remove precision 
    else { 
    that = that.toFixed(0); 
    } 

    // get the string now that precision is correct 
    var fnum = that.toString(); 

    // format has comma, then compute commas 
    if (hasComma) { 
    // remove precision for computation 
    psplit = fnum.split(’.'); 

    var cnum = psplit[0], 
     parr = [], 
     j = cnum.length, 
     m = Math.floor(j/3), 
     n = cnum.length % 3 || 3; // n cannot be ZERO or causes infinite loop 

    // break the number into chunks of 3 digits; first chunk may be less than 3 
    for (var i = 0; i < j; i += n) { 
     if (i != 0) {n = 3;} 
     parr[parr.length] = cnum.substr(i, n); 
     m -= 1; 
    } 

    // put chunks back together, separated by comma 
    fnum = parr.join(’,'); 

    // add the precision back in 
    if (psplit[1]) {fnum += ‘.’ + psplit[1];} 
    } 

    // replace the number portion of the format with fnum 
    return format.replace(/[\d,?\.?]+/, fnum); 
}; 
1

Globalize庫是實現您的目標的最簡單方法。這使您可以指定一種格式並說明不同文化處理數字分組的方式。例如:

Globalize.format(Math.round(16810900.211233)) 

結果16,810,900

+0

不會round()16810900.711233將結果更改爲16810901?我認爲parseInt()或Math.Floor()可能在這裏工作,除非OP想要向上/向下舍入。 – 2012-04-27 14:47:19

+0

@Chris Right - 我不確定OP需要什麼。實際上,Globalize具有一個數字格式參數,它假定四捨五入,所以'Globalize.format(16810900.711233,'n0')'產生'16,810,901'。 – 2012-04-27 15:36:17

1
$("p").text(function(i, v) { 
    if (v == "-") 
     return v; // or whatever you want to to with these 
    v = v.split(".")[0]; // remove anything after the first "." 
    v = v.replace(/[^\d]/g, ""); // remove non-numeric characters 
    var group = 3; 
    var min = v.length > 4 ? group : 4; // format thousands without separator 
    for (var res=[], i=v.length; i>min; i-=group) 
     res.push(v.substr(i-group, group)); 
    res.push(v.substr(0,i)); 
    return res.reverse().join("."); 
}); 

這將格式化數字,即便在4個字符的數字沒有設置千個分隔符。

+0

嗨Bergi,這工作正常。但有些數字不格式請檢查此鏈接http://jsfiddle.net/sureshpattu/Hrac3/3/我在這裏試過 – 2012-04-28 04:02:28

+0

通常數千沒有格式化分隔符。我的[新版本](http://jsfiddle.net/Hrac3/4/)使這一點更加明顯。 – Bergi 2012-04-29 20:17:22