mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-23 14:50:42 +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) {
|
abstract class CurrencyUnit(val value:Double) {
|
||||||
def toStringWithoutCurrencyLabel : String = CurrencyUnits.currencyFormatter(value)
|
def toStringWithoutCurrencyLabel : String = CurrencyUnits.currencyFormatter(value)
|
||||||
def ==(c : CurrencyUnit) = {
|
def ==(c : CurrencyUnit) : Boolean = {
|
||||||
require (c != null, "Currency units cannot be null")
|
/*require (c != null, "Currency units cannot be null")*/
|
||||||
CurrencyUnits.toSatoshis(this).value == CurrencyUnits.toSatoshis(c).value
|
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) {
|
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 {
|
object CurrencyUnits {
|
||||||
val oneSatoshi = Satoshis(1)
|
def oneSatoshi = Satoshis(1)
|
||||||
val oneMilliBit = Satoshis(100000)
|
def oneMilliBit = Satoshis(100000)
|
||||||
val tenMilliBits = Satoshis(1000000)
|
def tenMilliBits = Satoshis(1000000)
|
||||||
val oneHundredMilliBits = Satoshis(10000000)
|
def oneHundredMilliBits = Satoshis(10000000)
|
||||||
|
|
||||||
|
def oneBTC = Bitcoins(1)
|
||||||
|
|
||||||
/*considering the scalar for a 1 BTC to be 1*/
|
/*considering the scalar for a 1 BTC to be 1*/
|
||||||
val satoshiScalar = 0.00000001
|
val satoshiScalar = 0.00000001
|
||||||
val bitsScalar = 0.000001
|
val bitsScalar = 0.000001
|
||||||
val bitcoinScalar = 1
|
val bitcoinScalar = 1
|
||||||
val milliBitcoinScalar = 0.001
|
val milliBitcoinScalar = 0.001
|
||||||
|
|
||||||
def satoshisToBits(satoshis: Satoshis): Bits = {
|
def satoshisToBits(satoshis: Satoshis): Bits = {
|
||||||
Bits((satoshis.value * satoshiScalar) / bitsScalar)
|
Bits((satoshis.value * satoshiScalar) / bitsScalar)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue