Minor: allow double constants to be used to create Amounts, you can now write 35.99.DOLLARS to mean $35.99

This commit is contained in:
Mike Hearn 2016-02-03 12:48:08 +01:00
parent 0417e24fae
commit fbd4b60834

View File

@ -35,6 +35,8 @@ val Int.DOLLARS: Amount get() = Amount(this.toLong() * 100, USD)
val Int.POUNDS: Amount get() = Amount(this.toLong() * 100, GBP)
val Int.SWISS_FRANCS: Amount get() = Amount(this.toLong() * 100, CHF)
val Double.DOLLARS: Amount get() = Amount((this * 100).toLong(), USD)
//// Requirements /////////////////////////////////////////////////////////////////////////////////////////////////////
class Requirements {
@ -89,6 +91,8 @@ data class Amount(val pennies: Long, val currency: Currency) : Comparable<Amount
operator fun div(other: Long): Amount = Amount(pennies / other, currency)
operator fun times(other: Long): Amount = Amount(Math.multiplyExact(pennies, other), currency)
operator fun div(other: Int): Amount = Amount(pennies / other, currency)
operator fun times(other: Int): Amount = Amount(Math.multiplyExact(pennies, other.toLong()), currency)
override fun toString(): String = currency.currencyCode + " " + (BigDecimal(pennies) / BigDecimal(100)).toPlainString()