Skip to content

Instantly share code, notes, and snippets.

@townivan
Last active November 1, 2017 18:50
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/423fa65af75b1d017775c46d36ec7ba7 to your computer and use it in GitHub Desktop.
Save townivan/423fa65af75b1d017775c46d36ec7ba7 to your computer and use it in GitHub Desktop.
updated JavaScript function for displaying currency (allows rounding up to next whole cent or dollar)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
// 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);
};
}
})();
console.log( icur(1000.021) );
console.log( icur(1000.021, "c") );
console.log( icur(1000.021, "d") );
console.log( icur(1000.021, "cu") );
console.log( icur(1000.021, "du") );
console.log( icur(1000.021, "duc") );
console.log( icur(1000.021, "ducu") );
function icur(a,b){
// arguments: a=the number b=the formatting option
// c (default) 1000.021 > $1,000.02
// d (dollar only) 1000.021 > $1,000
// cu (cents rounded up to the next whole cent) 1000.021 > $1,000.03
// du (dollars rounded up the next whole dollar - no cents) 1000.021 > $1,001
// duc (dollars rounded up the next whole dollar - with cents) 1000.021 > $1,001.00
// ducu (dollars rounded up the next whole dollar after cents rounded up to next whole cent - with cents) 1000.021 > $1,001.00
let displayCents = true;
let roundCentsUp = false;
let roundDollarsUp = false;
if (typeof b === 'undefined') { // argument b is missing (or someone passed b declared as undefined)
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 2 });
a = formatter.format(a); // 1000.021 => $1,000.02
return a;
}
else{ // argument b received
// c (default) 1000.021 > $1,000.02
if (b === "c"){
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 2 });
a = formatter.format(a); // 1000.021 => $1,000.02
return a;
}
// d (dollar only) 1000.021 > $1,000
if (b === "d"){
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 0, maximumFractionDigits: 0 });
a = formatter.format(a); // 1000.021 => $1,000.00
return a;
}
// cu (cents rounded up to the next whole cent) 1000.021 > $1,000.03
if (b === "cu"){
a = Math.ceil10(a, -2); // the crucial cent up rounding
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 2 });
a = formatter.format(a); // 1000.021 => $1,000.03
return a;
}
// du (dollars rounded up the next whole dollar - no cents) 1000.021 > $1,001
if (b === "du"){
a = Math.ceil10(a, 0); // the crucial dollar up rounding
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 0, maximumFractionDigits: 0 });
a = formatter.format(a); // 1000.021 => $1,001
return a;
}
// duc (dollars rounded up the next whole dollar - with cents) 1000.021 > $1,001.00
if (b === "duc"){
a = Math.ceil10(a, 0); // the crucial dollar up rounding
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 2 });
a = formatter.format(a); // 1000.021 => $1,001.00
return a;
}
// ducu (dollars rounded up the next whole dollar after cents are rounded up the next whole cent) 1000.021 > $1,001.00
if (b === "ducu"){
a = Math.ceil10(a, -2); // the crucial cent up rounding
a = Math.ceil10(a, 0); // the crucial dollar up rounding
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency', currency: 'USD',
minimumFractionDigits: 2 });
a = formatter.format(a); // 1000.021 => $1,001.00
return a;
}
}
return "invalid formatting argument";
}// end icur(a,b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment