Created
June 5, 2018 05:24
-
-
Save sayanriju/6f39d06970bbef9429b5da284f68e9e2 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
/** | |
* Formats a number to a string in the lakh/crore (Indian) format. | |
* Also, add/truncate to two decimal places, as expected for a currency | |
* @param num The number to format | |
*/ | |
function desiCurrency(num) { | |
let ret = num.toFixed(2) | |
ret = `${ret}` || "" | |
// works for integer and floating as well | |
let n1 = ret.split(".") | |
const n2 = n1[1] || null | |
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, "$1,") | |
ret = n2 ? `${n1}.${n2}` : n1 | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment