How to represent currency in TypeScript

1 Answer

0 votes
/*---------------------------------------------------------
  Description:
    Demonstrates how to represent and display currency
    values in idiomatic TypeScript using Intl.NumberFormat.
---------------------------------------------------------*/

const usd = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD"
});

// Price and tax rate
const price: number = 199.99;
const taxRate: number = 0.17; // 17%

// Calculate tax and total
const taxAmount: number = price * taxRate;
const total: number = price + taxAmount;

// Output with $ and % signs
console.log("Price: " + usd.format(price));
console.log("Tax Rate: 17%");
console.log("Tax Amount: " + usd.format(taxAmount));
console.log("Total: " + usd.format(total));


/*
run:

Price: $199.99
Tax Rate: 17%
Tax Amount: $34.00
Total: $233.99

*/

 



answered 3 hours ago by avibootz
...