mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-22 14:33:06 +01:00
Adding infix operators to compare currency units
This commit is contained in:
parent
dabe6396e5
commit
6dd4e6d622
1 changed files with 30 additions and 6 deletions
|
@ -4,10 +4,31 @@ import scala.math.BigDecimal.RoundingMode
|
|||
|
||||
abstract class CurrencyUnit(val value:Double) {
|
||||
def toStringWithoutCurrencyLabel : String = CurrencyUnits.currencyFormatter(value)
|
||||
def ==(c : CurrencyUnit) = {
|
||||
require (c != null, "Currency units cannot be null")
|
||||
def ==(c : CurrencyUnit) : Boolean = {
|
||||
/*require (c != null, "Currency units cannot be null")*/
|
||||
CurrencyUnits.toSatoshis(this).value == CurrencyUnits.toSatoshis(c).value
|
||||
}
|
||||
|
||||
def >=(c : CurrencyUnit) : Boolean = {
|
||||
CurrencyUnits.toSatoshis(this).value >= CurrencyUnits.toSatoshis(c).value
|
||||
}
|
||||
|
||||
def >(c : CurrencyUnit) : Boolean = {
|
||||
CurrencyUnits.toSatoshis(this).value > CurrencyUnits.toSatoshis(c).value
|
||||
}
|
||||
|
||||
def <(c : CurrencyUnit) : Boolean = {
|
||||
CurrencyUnits.toSatoshis(this).value < CurrencyUnits.toSatoshis(c).value
|
||||
}
|
||||
|
||||
def <=(c : CurrencyUnit) : Boolean = {
|
||||
/*require (c != null, "Currency units cannot be null")*/
|
||||
CurrencyUnits.toSatoshis(this).value <= CurrencyUnits.toSatoshis(c).value
|
||||
}
|
||||
|
||||
def !=(c : CurrencyUnit) : Boolean = {
|
||||
!(this == c)
|
||||
}
|
||||
}
|
||||
|
||||
case class Satoshis(override val value: Double) extends CurrencyUnit(value) {
|
||||
|
@ -29,16 +50,19 @@ case class MilliBitcoins(override val value : Double) extends CurrencyUnit(value
|
|||
}
|
||||
|
||||
object CurrencyUnits {
|
||||
val oneSatoshi = Satoshis(1)
|
||||
val oneMilliBit = Satoshis(100000)
|
||||
val tenMilliBits = Satoshis(1000000)
|
||||
val oneHundredMilliBits = Satoshis(10000000)
|
||||
def oneSatoshi = Satoshis(1)
|
||||
def oneMilliBit = Satoshis(100000)
|
||||
def tenMilliBits = Satoshis(1000000)
|
||||
def oneHundredMilliBits = Satoshis(10000000)
|
||||
|
||||
def oneBTC = Bitcoins(1)
|
||||
|
||||
/*considering the scalar for a 1 BTC to be 1*/
|
||||
val satoshiScalar = 0.00000001
|
||||
val bitsScalar = 0.000001
|
||||
val bitcoinScalar = 1
|
||||
val milliBitcoinScalar = 0.001
|
||||
|
||||
def satoshisToBits(satoshis: Satoshis): Bits = {
|
||||
Bits((satoshis.value * satoshiScalar) / bitsScalar)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue