Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Short and fast solution (works everywhere!)</h2> <pre><code>(12345.67).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&amp;,'); // 12,345.67 </code></pre> <p>The idea behind this solution is replacing matched sections with first match and comma, i.e. <code>'$&amp;,'</code>. The matching is done using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-lookahead" rel="noreferrer">lookahead approach</a>. You may read the expression as <em>"match a number if it is followed by a sequence of three number sets (one or more) and a dot"</em>.</p> <p><strong>TESTS:</strong></p> <pre><code>1 --&gt; "1.00" 12 --&gt; "12.00" 123 --&gt; "123.00" 1234 --&gt; "1,234.00" 12345 --&gt; "12,345.00" 123456 --&gt; "123,456.00" 1234567 --&gt; "1,234,567.00" 12345.67 --&gt; "12,345.67" </code></pre> <p><strong>DEMO:</strong> <a href="http://jsfiddle.net/hAfMM/9571/" rel="noreferrer">http://jsfiddle.net/hAfMM/9571/</a></p> <hr> <h2>Extended short solution</h2> <p>You can also extend the prototype of <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number" rel="noreferrer"><code>Number</code></a> object to add additional support of any number of decimals <code>[0 .. n]</code> and the size of number groups <code>[0 .. x]</code>:</p> <pre><code>/** * Number.prototype.format(n, x) * * @param integer n: length of decimal * @param integer x: length of sections */ Number.prototype.format = function(n, x) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n &gt; 0 ? '\\.' : '$') + ')'; return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&amp;,'); }; 1234..format(); // "1,234" 12345..format(2); // "12,345.00" 123456.7.format(3, 2); // "12,34,56.700" 123456.789.format(2, 4); // "12,3456.79" </code></pre> <p><strong>DEMO / TESTS:</strong> <a href="http://jsfiddle.net/hAfMM/435/" rel="noreferrer">http://jsfiddle.net/hAfMM/435/</a></p> <hr> <h2>Super extended short solution</h2> <p>In this <a href="https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript/14428340#comment34151293_14428340">super extended version</a> you may set different delimiter types:</p> <pre><code>/** * Number.prototype.format(n, x, s, c) * * @param integer n: length of decimal * @param integer x: length of whole part * @param mixed s: sections delimiter * @param mixed c: decimal delimiter */ Number.prototype.format = function(n, x, s, c) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n &gt; 0 ? '\\D' : '$') + ')', num = this.toFixed(Math.max(0, ~~n)); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&amp;' + (s || ',')); }; 12345678.9.format(2, 3, '.', ','); // "12.345.678,90" 123456.789.format(4, 4, ' ', ':'); // "12 3456:7890" 12345678.9.format(0, 3, '-'); // "12-345-679" </code></pre> <p><strong>DEMO / TESTS:</strong> <a href="http://jsfiddle.net/hAfMM/612/" rel="noreferrer">http://jsfiddle.net/hAfMM/612/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload