- document.write('const { abs, floor, round } = Math');
- document.write('const CENT = 0.005');
- document.write('const TEN = 10');
- document.write('const HUNDRED = 100');
- document.write('const THOUSAND = 1000');
- document.write('const MILLION = 1000000');
- document.write('const BILLION = 1000000000');
- document.write('const TRILLION = 1000000000000');
- document.write('');
- document.write('function truncate(number: number, base: number) {');
- document.write(' const divided = number / base');
- document.write(' return divided < HUNDRED ? floor(divided * 10) / 10 : floor(divided)');
- document.write('}');
- document.write('');
- document.write('function rounded(number: number, base: number) {');
- document.write(' const divided = number / base');
- document.write(' return divided < HUNDRED ? round(divided * 10) / 10 : round(divided)');
- document.write('}');
- document.write('');
- document.write('export function abbreviateCurrency(number: number): string {');
- document.write(' if (typeof number !== \'number\') {');
- document.write(' const parsed = parseFloat(number)');
- document.write(' if (isNaN(parsed)) throw Error(`Expected input "${number}" to be a number`)');
- document.write(' number = parsed');
- document.write(' }');
- document.write(' const isNegative = number < 0');
- document.write(' const absoluteValue = abs(number)');
- document.write(' let abbreviated: string');
- document.write('');
- document.write(' if (absoluteValue < CENT) return \'$0\'');
- document.write(' else if (absoluteValue < TEN) abbreviated = absoluteValue.toFixed(2)');
- document.write(' else if (absoluteValue < THOUSAND) abbreviated = absoluteValue.toFixed(0)');
- document.write(' else if (absoluteValue < MILLION) abbreviated = truncate(absoluteValue, THOUSAND) + \'K\'');
- document.write(' else if (absoluteValue < BILLION) abbreviated = truncate(absoluteValue, MILLION) + \'M\'');
- document.write(' else if (absoluteValue < TRILLION) abbreviated = truncate(absoluteValue, BILLION) + \'B\'');
- document.write(' else abbreviated = truncate(absoluteValue, TRILLION) + \'T\'');
- document.write('');
- document.write(' return isNegative ? \'-$\' + abbreviated : \'$\' + abbreviated');
- document.write('}');
- document.write('');
- document.write('export function abbreviateCurrencyRounded(number: number): string {');
- document.write(' if (typeof number !== \'number\') {');
- document.write(' const parsed = parseFloat(number)');
- document.write(' if (isNaN(parsed)) throw Error(`Expected input "${number}" to be a number`)');
- document.write(' number = parsed');
- document.write(' }');
- document.write(' const isNegative = number < 0');
- document.write(' const absoluteValue = abs(number)');
- document.write(' let abbreviated: string');
- document.write('');
- document.write(' if (absoluteValue < CENT) return \'$0\'');
- document.write(' else if (absoluteValue < TEN) abbreviated = absoluteValue.toFixed(2)');
- document.write(' else if (absoluteValue < THOUSAND) abbreviated = absoluteValue.toFixed(0)');
- document.write(' else if (absoluteValue < MILLION) abbreviated = rounded(absoluteValue, THOUSAND) + \'K\'');
- document.write(' else if (absoluteValue < BILLION) abbreviated = rounded(absoluteValue, MILLION) + \'M\'');
- document.write(' else if (absoluteValue < TRILLION) abbreviated = rounded(absoluteValue, BILLION) + \'B\'');
- document.write(' else abbreviated = rounded(absoluteValue, TRILLION) + \'T\'');
- document.write('');
- document.write(' return isNegative ? \'-$\' + abbreviated : \'$\' + abbreviated');
- document.write('}');
- document.write('');
- document.write('function truncateTwoDecimal(number: number, base: number) {');
- document.write(' const divided = number / base');
- document.write(' return divided < HUNDRED ? ((divided * 10) / 10).toFixed(2) : divided.toFixed(2)');
- document.write('}');
- document.write('');
- document.write('export function abbreviateCurrencyTwoDecimal(number: number): string {');
- document.write(' if (typeof number !== \'number\') {');
- document.write(' const parsed = parseFloat(number)');
- document.write(' if (isNaN(parsed)) throw Error(`Expected input "${number}" to be a number`)');
- document.write(' number = parsed');
- document.write(' }');
- document.write(' const isNegative = number < 0');
- document.write(' const absoluteValue = abs(number)');
- document.write(' let abbreviated: string');
- document.write('');
- document.write(' if (absoluteValue < CENT) return \'$0\'');
- document.write(' else if (absoluteValue < TEN) abbreviated = absoluteValue.toFixed(2)');
- document.write(' else if (absoluteValue < THOUSAND) abbreviated = absoluteValue.toFixed(0)');
- document.write(' else if (absoluteValue < MILLION) abbreviated = truncateTwoDecimal(absoluteValue, THOUSAND) + \'K\'');
- document.write(' else if (absoluteValue < BILLION) abbreviated = truncateTwoDecimal(absoluteValue, MILLION) + \'M\'');
- document.write(' else if (absoluteValue < TRILLION) abbreviated = truncateTwoDecimal(absoluteValue, BILLION) + \'B\'');
- document.write(' else abbreviated = truncateTwoDecimal(absoluteValue, TRILLION) + \'T\'');
- document.write('');
- document.write(' return isNegative ? \'-$\' + abbreviated : \'$\' + abbreviated');
- document.write('}');
[text] asd
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: