Skip to content

Instantly share code, notes, and snippets.

@sayanriju
Created June 5, 2018 05:24
Show Gist options
  • Save sayanriju/6f39d06970bbef9429b5da284f68e9e2 to your computer and use it in GitHub Desktop.
Save sayanriju/6f39d06970bbef9429b5da284f68e9e2 to your computer and use it in GitHub Desktop.
/**
* 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