Created
February 2, 2012 19:25
-
-
Save lunaroja/1725242 to your computer and use it in GitHub Desktop.
Quick Currency formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* JavaScript: US currency helper functions | |
-------------------------------------------------------*/ | |
/** | |
* Formats any number, string or HTML string to a number with two trailing decimals | |
* @param {String} html Any number, string or HTML string | |
* @return {Number} Returns a number with two trailing decimals | |
*/ | |
var toCurrency = function(html) { | |
var currency = html.match(/[\d\.]+/g); | |
currency = currency.join(''); | |
return parseFloat(currency).toFixed(2); | |
} | |
// toCurrency('<span class="sign">$</span><span class="dollar">50</span><span class="decimal">.</span><span class="cents">00</span>'); | |
// toCurrency('50'); | |
// returns number 50.00 | |
/** | |
* Formats any number to a predifined HTML string | |
* @param {Number} num Any number | |
* @return {String} Returns an HTML string with spans and classes for sign, dollar, decimal, cents | |
*/ | |
var currencyToHTML = function(num) { | |
num = num.toString(); | |
num = num.split('.'); | |
var dollar = num[0]; | |
var cents = (num[1] === undefined) ? '00' : num[1]; | |
var format = '<span class="sign">$</span>'; | |
format += '<span class="dollar">' + dollar + '</span>'; | |
format += '<span class="decimal">.</span>'; | |
format += '<span class="cents">' + cents + '</span>'; | |
return format; | |
} | |
// currencyToHTML('50.00'); | |
// returns string'<span class="sign">$</span><span class="dollar">50</span><span class="decimal">.</span><span class="cents">00</span>') | |
/** | |
* Adds two Numbers and returns formatted value | |
* @param {Number} num1 Initial number | |
* @param {Number} num2 Number to be added | |
* @return {String} Returns a value with two trailing decimals | |
*/ | |
var sumCurrency = function(num1, num2) { | |
num1 = isNaN(num1) || num1 === '' || num1 === null ? 0.00 : num1; | |
num2 = isNaN(num2) || num2 === '' || num2 === null ? 0.00 : num2; | |
var total = num1 + num2; | |
return parseFloat(total).toFixed(2); | |
} | |
// sumCurrency('50','9.99'); | |
// returns number 59.99 | |
/** | |
* Substracs two Numbers and returns formatted value | |
* @param {Number} num1 Initial number | |
* @param {Number} num2 Number to be subracted | |
* @return {String} Returns a value with two trailing decimals | |
*/ | |
var subCurrency = function(num1, num2) { | |
num1 = isNaN(num1) || num1 === '' || num1 === null ? 0.00 : num1; | |
num2 = isNaN(num2) || num2 === '' || num2 === null ? 0.00 : num2; | |
var total = num1 - num2; | |
return parseFloat(total).toFixed(2); | |
} | |
// subCurrency('50','9.99'); | |
// returns number 50.01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment