Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Payment {
- constructor(amount, currency) {
- this.amount = amount;
- this.currency = currency;
- }
- get localeMap() {
- return {
- USD: "en-US",
- EUR: "de-DE",
- JPY: "jp-JP",
- GBP: "en-GB"
- };
- }
- toString() {
- const locale = this.localeMap[this.currency];
- const intl = new Intl.NumberFormat(locale, {
- style: "currency",
- currency: this.currency
- });
- return intl.format(this.amount);
- }
- }
- const payments = [
- new Payment(0, "USD"),
- new Payment(10.50, "EUR"),
- new Payment(15.123, "JPY"),
- new Payment(600.314578, "GBP")
- ];
- /**
- * This loop will print:
- * $0.00
- * € 10.50
- * JP¥ 15
- * £ 600.31
- **/
- payments.forEach(payment => {
- console.log(payment.toString());
- });
Add Comment
Please, Sign In to add comment