How to represent currency in Kotlin

1 Answer

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

import java.math.BigDecimal
import java.text.NumberFormat
import java.util.Locale

fun main() {

    // Currency formatter for US dollars
    val usd = NumberFormat.getCurrencyInstance(Locale.US)

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

    // Calculate tax and total
    val taxAmount = price.multiply(taxRate)
    val total = price.add(taxAmount)

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


/*
run:

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

*/

 



answered 12 hours ago by avibootz
...