Skip to content

Instantly share code, notes, and snippets.

@mabsboza
Created November 27, 2018 23:44
Show Gist options
  • Save mabsboza/0188dcb9f8fc2011719703d45b8d617d to your computer and use it in GitHub Desktop.
Save mabsboza/0188dcb9f8fc2011719703d45b8d617d to your computer and use it in GitHub Desktop.
// 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