Created
November 27, 2018 23:44
-
-
Save mabsboza/0188dcb9f8fc2011719703d45b8d617d to your computer and use it in GitHub Desktop.
This file contains 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
// Function to format amounts with commas and 2 decimals | |
const FormatToLocalString = (num) => { | |
if (num === null || undefined) { | |
return num = 0.00; | |
} else { | |
//Check if amount is float. | |
let isFloat = Number(num) === num && num % 1 !== 0; | |
if (isFloat) { | |
// Format amount with 2 decimals | |
num = Math.round(num * 100) / 100; | |
} | |
// Format amount with comma | |
return num.toLocaleString(navigator.language, { | |
minimumFractionDigits: 2, | |
maximumFractionDigits: 4 | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment