mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2024-11-20 02:11:40 +01:00
Round Bitcoins to nearest Satoshi (#4154)
This commit is contained in:
parent
23f359821f
commit
c379cf4a73
@ -6,6 +6,8 @@ import org.bitcoins.core.serializers.RawSatoshisSerializer
|
|||||||
import org.bitcoins.crypto.{Factory, NetworkElement}
|
import org.bitcoins.crypto.{Factory, NetworkElement}
|
||||||
import scodec.bits.ByteVector
|
import scodec.bits.ByteVector
|
||||||
|
|
||||||
|
import scala.math.BigDecimal.RoundingMode
|
||||||
|
import scala.math.BigDecimal.RoundingMode.RoundingMode
|
||||||
import scala.math.Numeric
|
import scala.math.Numeric
|
||||||
import scala.util.{Failure, Success, Try}
|
import scala.util.{Failure, Success, Try}
|
||||||
|
|
||||||
@ -170,7 +172,14 @@ object Bitcoins extends BaseNumbers[Bitcoins] with Bounded[Bitcoins] {
|
|||||||
Bitcoins(b)
|
Bitcoins(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
def apply(underlying: BigDecimal): Bitcoins = BitcoinsImpl(underlying)
|
def apply(
|
||||||
|
underlying: BigDecimal,
|
||||||
|
roundingMode: RoundingMode = RoundingMode.DOWN): Bitcoins = {
|
||||||
|
// Bitcoin can't represent amounts lower than a satoshi
|
||||||
|
// so we need to round to the nearest satoshi
|
||||||
|
val rounded = underlying.setScale(8, roundingMode)
|
||||||
|
BitcoinsImpl(rounded)
|
||||||
|
}
|
||||||
|
|
||||||
private case class BitcoinsImpl(underlying: BigDecimal) extends Bitcoins
|
private case class BitcoinsImpl(underlying: BigDecimal) extends Bitcoins
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user