Last active
September 24, 2019 08:01
-
-
Save maarten00/5f62236632bf153267dd to your computer and use it in GitHub Desktop.
Prettyprice Javascript
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
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