July 2018
Intermediate to advanced
400 pages
12h 14m
English
Rather than working with 4.1899999999999995 pieces of gold, you will round the value up to 4.19. String’s format function can be used to round a double to a precision that you define. Update the performPurchase function to format the remaining balance amount:
Listing 8.5 Formatting a double (Tavern.kt)
...
fun performPurchase(price: Double) {
displayBalance()
val totalPurse = playerGold + (playerSilver / 100.0)
println("Total purse: $totalPurse")
println("Purchasing item for $price")
val remainingBalance = totalPurse - price
println("Remaining balance: ${"%.2f".format(remainingBalance)}")
}
...
The gold remaining in the purse is interpolated into the string using $, as you have seen before. But what follows ...
Read now
Unlock full access