Skip to content

有害部落格同好會

2006/01/25 / JavaScript, VBScript

數字格式化 Add Commas

資料來源:mredkj

Javascript 本身沒有 NumberFormat 之類的 function 可以用來將數字格式化

這裡有個簡單的 function,可以輕易做到 NumberFormat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<span style="color: #004101;">Add Commas</span>
<span style="color: #000699;">JavaScript</span>
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length &gt; 1 ? '.' + x[1] : '';
var rgx = /(d+)(d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}

<span style="color: #004101;">Add Commas</span> <span style="color: #000699;">JavaScript</span> function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length &gt; 1 ? '.' + x[1] : ''; var rgx = /(d+)(d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }

Change a number such as 1000 into a string 1,000. Pass the value as a string, and it will preserve zeros. 

Examples

1
2
3
4
5
6
7
8
9
10
11
addCommas(1000)
// 1,000
 
addCommas(1231.897243)
// 1,231.897243
 
addCommas('9999999.00')
// 9,999,999.00
 
addCommas(-500000.99)
// -500,000.99

addCommas(1000) // 1,000 addCommas(1231.897243) // 1,231.897243 addCommas('9999999.00') // 9,999,999.00 addCommas(-500000.99) // -500,000.99

Other separators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span style="color: #000699;">JavaScript</span>
function addSeparatorsNF(nStr, inD, outD, sep)
{
nStr += '';
var dpos = nStr.indexOf(inD);
var nStrEnd = '';
if (dpos != -1) {
nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
nStr = nStr.substring(0, dpos);
}
var rgx = /(d+)(d{3})/;
while (rgx.test(nStr)) {
nStr = nStr.replace(rgx, '$1' + sep + '$2');
}
return nStr + nStrEnd;
}

<span style="color: #000699;">JavaScript</span> function addSeparatorsNF(nStr, inD, outD, sep) { nStr += ''; var dpos = nStr.indexOf(inD); var nStrEnd = ''; if (dpos != -1) { nStrEnd = outD + nStr.substring(dpos + 1, nStr.length); nStr = nStr.substring(0, dpos); } var rgx = /(d+)(d{3})/; while (rgx.test(nStr)) { nStr = nStr.replace(rgx, '$1' + sep + '$2'); } return nStr + nStrEnd; }

addSeparatorsNF is part my comprehensive NumberFormat script, but if you only need separator formatting, then you can use the function by itself. It does not require the NumberFormat script.

Not every formatting style is the same. e.g. 1000 may be formatted as 1,000 or 1.000

So addSeparatorsNF gives you the ability to specify the input decimal character, the output decimal character, and the output separator character.
Arguments

To use addSeparatorsNF, you need to pass it the following arguments:

nStr: The number to be formatted, as a string or number. No validation is done, so don’t input a formatted number. If inD is something other than a period, then nStr must be passed in as a string.

inD: The decimal character for the input, such as ‘.’ for the number 100.2

outD: The decimal character for the output, such as ‘,’ for the number 100,2

sep: The separator character for the output, such as ‘,’ for the number 1,000.2

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
addSeparatorsNF(43211234.56, '.', '.', ',')
// 43,211,234.56
 
addSeparatorsNF('52093423.003', '.', ',', '.')
// 52.093.423,003
 
addSeparatorsNF('93432,8', ',', '.', ',')
// 93,432.8
 
addSeparatorsNF('584,567890', ',', '.', ',')
// 584.567890
 
addSeparatorsNF(-1.23e8, '.', '.', ',')
// -123,000,000

addSeparatorsNF(43211234.56, '.', '.', ',') // 43,211,234.56 addSeparatorsNF('52093423.003', '.', ',', '.') // 52.093.423,003 addSeparatorsNF('93432,8', ',', '.', ',') // 93,432.8 addSeparatorsNF('584,567890', ',', '.', ',') // 584.567890 addSeparatorsNF(-1.23e8, '.', '.', ',') // -123,000,000

Post navigation

Previous Post:

Ajax without using the XmlHttpRequest Object

Next Post:

Print Screen 鍵的更多用法

分類

  • 新奇有趣の搶先報導
    • Raspberry Pi
    • 手機相關
    • 推薦軟體
    • 新鮮有趣
    • 生活資訊
  • 想破腦袋の程式技巧
    • Oracle EBS
    • Database
    • Excel, VBA
    • php
    • JavaScript, VBScript
    • VS.NET
    • Others
    • Windows
    • SAP
  • 撩動心弦の短文小品
  • 聚沙成塔の理財守則
  • 不可不知の職場實錄
  • 剎那永恆の生活翦影

近期文章

  • 受保護的內容: 如何透過Personalize功能呼叫另一form來回傳值
  • Win10 / 8 / 7 無法安裝 SSD
  • 受保護的內容: 樹梅派+遠端連線
  • EBS R12 取得客戶的phone, email, URL資料
  • 受保護的內容: 管控Workflow Administrator Role

友站

  • Masaya396's 協奏曲
  • 老塗的咁仔店

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

Tag Cloud

你目前使用的瀏覽器不支援 HTML5 的 CANVAS 標籤。

  • SAP
  • WinXP
  • VBA
  • Win7
  • CSS
  • php
  • VB6
  • Oracle DB
  • HTC
  • LDAP
  • SQL
  • VB.Net
  • 感情
  • EBS 11.5.10
  • MySql
  • javascript
  • 管理
  • Oracle EBS
  • EBS 12.1.3
  • excel
© 2025 有害部落格同好會 - Powered by SimplyNews