Guest User

Untitled

a guest
May 27th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. class Payment {
  2.  
  3. constructor(amount, currency) {
  4. this.amount = amount;
  5. this.currency = currency;
  6. }
  7.  
  8. get localeMap() {
  9. return {
  10. USD: "en-US",
  11. EUR: "de-DE",
  12. JPY: "jp-JP",
  13. GBP: "en-GB"
  14. };
  15. }
  16.  
  17. toString() {
  18. const locale = this.localeMap[this.currency];
  19.  
  20. const intl = new Intl.NumberFormat(locale, {
  21. style: "currency",
  22. currency: this.currency
  23. });
  24.  
  25. return intl.format(this.amount);
  26. }
  27. }
  28.  
  29. const payments = [
  30. new Payment(0, "USD"),
  31. new Payment(10.50, "EUR"),
  32. new Payment(15.123, "JPY"),
  33. new Payment(600.314578, "GBP")
  34. ];
  35.  
  36. /**
  37. * This loop will print:
  38. * $0.00
  39. * € 10.50
  40. * JP¥ 15
  41. * £ 600.31
  42. **/
  43. payments.forEach(payment => {
  44. console.log(payment.toString());
  45. });
Add Comment
Please, Sign In to add comment