Last active
November 19, 2019 16:25
-
-
Save MMortari/6945d79975967f641bc3fd61b44c8660 to your computer and use it in GitHub Desktop.
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
const DinheiroMask = ({ prefix = "R$", decimal = 2, chunkDelimiter = '.', decimalDelimiter = ',', children }) => { | |
let total = ""; | |
let value = children; | |
if (!value) { | |
value = 0; | |
} | |
const result = '\\d(?=(\\d{3})+' + (decimal > 0 ? '\\D' : '$') + ')'; | |
const num = value.toFixed(Math.max(0, ~~decimal)); | |
total = prefix + ' ' + (decimalDelimiter ? num.replace('.', decimalDelimiter) : num).replace(new RegExp(result, 'g'), '$&' + chunkDelimiter); | |
return total; | |
} | |
export default DinheiroMask; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment