<span>100000</span> should be 100000
<br>
<span>$100,000</span> should be $100,000
<br>
<span>$100,000.00</span> should be $100,000.00
<br>
<br>
<span>1000000</span> should be 1M
<br>
<span>$1,000,000</span> should be $1M
<br>
<span>$1,500,000.00</span> should be $1.50M
<br>
<br>
<span>1000000000</span> should be 1B
<br>
<span>$1,000,000,000</span> should be $1B
<br>
<span>$1,500,000,000.00</span> should be $1.50B
<br>
<br>
<span>1000000000000</span> should be 1t
<br>
<span>$1,000,000,000,000</span> should be $1t
<br>
<span>$1,500,000,000,000.00</span> should be $1.50t
<br>
<br>
<span>1000000000000000</span> should be 1q
<br>
<span>$1,000,000,000,000,000</span> should be $1q
<br>
<span>$1,500,000,000,000,000.00</span> should be $1.50q
<br>
<br>
CSS
span {
color: red;
}
JavaScript
/**
* Shorten number/currency to millions, billions, etc.
* http://crusaders-of-the-lost-idols.wikia.com/wiki/Large_Number_Abbreviations
*
* @param {string|number} num Number to shorten.
* @param {number} [digits=0] The number of digits to appear after the decimal point.
* @returns {string}
*
* @example
* // returns '51M'
* shortenLargeNumber(51000000)
*
* @example
* // returns '$51M'
* shortenLargeNumber('$51,000,000')
*
* @example
* // returns 651
* shortenLargeNumber(651)
*
* @example
* // returns 0.12
* shortenLargeNumber(0.12345,2)
*/
function formatCurrency(num, currency, digits) {
// num can be a string or a number
// make sure it is treated as a number, not a string
num = num.toString();
num = Number(num.replace(/[^0-9\-\.]+/g, ""));
if (!digits) {
digits = 0;
}
num = addCommas(num.toFixed(digits));
return currency + num;
// helper function to add commas
function addCommas(str) {
str += '';
x = str.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
}
function shortenLargeNumber(num, digits) {
var isCurrency = false;
if (!digits) {
digits = 0;
}
// num can be a currency string or a number
// so you can pass it, e.g., "$200,000,000.00" as well as 200000000.00 or 200000000
num = (num.toString()).trim();
if (num.indexOf('$') === 0) {
isCurrency = true;
}
if (num.indexOf('.') > -1) {
digits = 2;
}
num = Number(num.replace(/[^0-9\-\.]+/g, '')); // replace anything that's NOT a number, - (minus) sign or . (dot), e.g. $200,000.00 becomes 200000; then convert to Number
// iterate
var units = ['k', 'M', 'B', 't', 'q'],
decimal;
if (num >= 1000000) {
for (var i = units.length - 1; i >= 0; i--) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.