Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active December 4, 2025 17:59
Show Gist options
  • Select an option

  • Save remarkablemark/323e424bab1254db96546b06310a6e2a to your computer and use it in GitHub Desktop.

Select an option

Save remarkablemark/323e424bab1254db96546b06310a6e2a to your computer and use it in GitHub Desktop.
/**
* Formats a number using compact notation with at most one decimal place (e.g., 1.2M or 123K).
*
* @param amount - The number to format.
* @returns The formatted number as a string.
*/
function formatNumber(amount) {
return new Intl.NumberFormat('en-US', {
notation: 'compact',
maximumFractionDigits: 1,
}).format(amount);
}
/**
* Formats a dollar currency with no decimal places (e.g., $123).
*
* @param amount - The number to format.
* @returns The formatted number as a string.
*/
function formatCurrency(amount) {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
}).format(amount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment