Skip to content

Instantly share code, notes, and snippets.

@townivan
Last active September 6, 2017 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save townivan/88cec443f37cf4ab1f64a3d5498375c5 to your computer and use it in GitHub Desktop.
Save townivan/88cec443f37cf4ab1f64a3d5498375c5 to your computer and use it in GitHub Desktop.
function myFormatCurrency(num, options) {
// Defaults to showing cents with normal rounding on the cent.
// myFormatCurrency(1000.021) -> "$1,000.02"
// options = 1 Don't show the cents
// myFormatCurrency(1000.021, 1) -> "$1,000"
// options = 2 Show the cents with cent rounding up the next whole cent
// myFormatCurrency(1000.021, 2) -> "$1,000.03"
if (typeof options === 'undefined') { centsYN = 1; roundUpYN = 0; } // default to 0 if omitted
if (options == 1){ centsYN = 0; roundUpYN = 0; }
if (options == 2){ centsYN = 1; roundUpYN = 1; }
if (options < 1 || options > 2){ centsYN = 1; roundUpYN = 0; } // default settings
if(isNaN(num))
num = "0";
var numOrig = num;
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
num = Math.floor(num/100).toString();
var realcents = (numOrig % 1)
var cents = realcents * 100;
cents = Math.round10(cents, -2); // new addition to better handle JavaScript floating number handling
var centsNormalRound = Math.round(cents);
var centsRoundedUp = Math.ceil(cents);
if (roundUpYN === 1){ cents = centsRoundedUp }
else { cents = centsNormalRound}
if(cents<10){
cents = "0" + cents; // make it two digits
}
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
if (centsYN==0){
return (((sign)?'':'-') + '$' + num );
}
if (centsYN==1){
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
}// end myFormatCurrency function
// https://stackoverflow.com/questions/42109818/reliable-js-rounding-numbers-with-tofixed2-of-a-3-decimal-number
// Closure
(function() {
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Decimal round
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Decimal floor
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Decimal ceil
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment