Skip to content

Instantly share code, notes, and snippets.

@maarten00
Last active September 24, 2019 08:01
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 maarten00/5f62236632bf153267dd to your computer and use it in GitHub Desktop.
Save maarten00/5f62236632bf153267dd to your computer and use it in GitHub Desktop.
Prettyprice Javascript
export const prettyPrice = price => {
price = parseFloat(price);
price = formatMoney(price);
price = addDecimalSeperators(price);
price = price.replace(",00", ",-");
return price;
};
const formatMoney = (price, length, decimalDelimiter, sectionDelimiter) => {
let c, d, t, s, i, j;
c = isNaN(length = Math.abs(length)) ? 2 : length;
d = decimalDelimiter === undefined ? "," : decimalDelimiter;
t = sectionDelimiter === undefined ? "." : sectionDelimiter;
s = price < 0 ? "-" : "";
i = parseInt(price = Math.abs(+price || 0).toFixed(c)) + "";
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(price - i).toFixed(c).slice(2) : "");
};
const addDecimalSeperators = number => {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment