Skip to content

Instantly share code, notes, and snippets.

@zed-wong
Last active December 11, 2022 13:32
Show Gist options
  • Save zed-wong/795f70a91f16f5ad2bc8fe92cf59f7f7 to your computer and use it in GitHub Desktop.
Save zed-wong/795f70a91f16f5ad2bc8fe92cf59f7f7 to your computer and use it in GitHub Desktop.
Ts utils functions
// $1.23
export const formatUSMoney = (x: string | number) => {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(Number(x))
}
// $123.23M
export const formatCompactUSD = (x: number) => {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumSignificantDigits: 2, notation: "compact" , compactDisplay: "short" }).format(x)
}
// 12.12%
export const formatPercentage = (x: number) => {
return Number(x/100).toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2})
}
// 2022/12/09
export const getToday = (sub: number = 0) => {
const d = new Date()
d.setDate(d.getDate()-sub)
return d.toJSON().slice(0,10).replace(/-/g,'/');
}
// format decimal when necessary
export const formatDecimals = (s: string|number, n: number) => {
return Math.floor(Number(s)*10**n)/10**n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment