How to represent currency in Scala

1 Answer

0 votes
/*---------------------------------------------------------
  Description:
    Demonstrates how to represent and display currency
    values in Scala using BigDecimal and formatting.
---------------------------------------------------------*/

object CurrencyExample {
  def main(args: Array[String]): Unit = {

    // Price and tax rate
    val price: BigDecimal = BigDecimal("199.99")
    val taxRate: BigDecimal = BigDecimal("0.17") // 17%

    // Calculate tax and total
    val taxAmount: BigDecimal = price * taxRate
    val total: BigDecimal = price + taxAmount

    // Output with $ and % signs
    println(f"Price: $$${price}%.2f")
    println("Tax Rate: 17%")
    println(f"Tax Amount: $$${taxAmount}%.2f")
    println(f"Total: $$${total}%.2f")
  }
}


/*
run:

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

*/

 



answered 12 hours ago by avibootz
...