Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement formatCurrencyWithNames #17

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ export function formatCurrency(
locale: string,
raw: boolean
): string;

// format currency with names
export function formatCurrencyWithNames(
amount: number,
isoCode: string,
locale: string
): string;
34 changes: 34 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,37 @@ export function formatCurrency(amount, isoCode, locale = "en", raw = false) {
}
}
}

export function formatCurrencyWithNames(amount, isoCode, locale = "en") {
isoCode = isoCode.toUpperCase();
if (currentISOCode !== isoCode || currentLocale != locale) {
currentISOCode = isoCode;
currentLocale = locale;

// Formatters are tied to currency code, we try to initialize as infrequently as possible.
initializeFormatters(isoCode, locale);
}

const absPrice = Math.abs(Number(amount));
let price = 0;
let suffix = "";
if (absPrice >= 1.0e9) {
// If Billion
price = absPrice / 1.0e9;
suffix = "B";
} else if (abs >= 1.0e6) {
// If Million
price = absPrice / 1.0e6;
suffix = "M";
} else if (absPrice >= 1.0e3) {
// If Thousands
price = absPrice / 1.0e3;
suffix = "K";
}
if (isCrypto(isoCode)) {
price = Number(price.toFixed(3));
return `${price}${suffix} ${isoCode}`;
} else {
return formatCurrency(price, isoCode, locale, false) + suffix;
}
}
28 changes: 27 additions & 1 deletion src/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { formatCurrency, isCrypto, clearCache } from "./index";
import {
formatCurrency,
isCrypto,
clearCache,
formatCurrencyWithNames
} from "./index";

test("isCrypto", () => {
expect(isCrypto("BTC")).toBe(true);
Expand Down Expand Up @@ -148,3 +153,24 @@ describe("Intl.NumberFormat not supported", () => {
});
});
});

describe("large number", () => {
describe("Billion", () => {
const billionVal1 = 9.101222e9;
const billionResVal1 = "9.101B";
const billionVal2 = 9e9;
const billionResVal2 = "9B";

test("format USD", () => {
expect(formatCurrencyWithNames(billionVal1, "USD", "en")).toEqual(
"USD " + billionResVal1
);
});

test("format BTC", () => {
expect(formatCurrencyWithNames(billionVal2, "BTC", "en")).toEqual(
billionResVal2 + " BTC"
);
});
});
});