File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed
main/scala/fr/acinq/bitcoin/scalacompat
test/scala/fr/acinq/bitcoin/scalacompat Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change 11package fr .acinq .bitcoin .scalacompat
22
3+ import fr .acinq .bitcoin .scalacompat .BtcAmount .{btcFormat , milliBtcFormat }
4+
5+ import java .text .{DecimalFormat , DecimalFormatSymbols }
6+ import java .util .Locale
7+
38sealed trait BtcAmount
49
510case class Satoshi (private val underlying : Long ) extends BtcAmount with Ordered [Satoshi ] {
@@ -52,7 +57,7 @@ case class MilliBtc(private val underlying: BigDecimal) extends BtcAmount with O
5257 def toBigDecimal : BigDecimal = underlying
5358 def toDouble : Double = underlying.toDouble
5459 def toLong : Long = underlying.toLong
55- override def toString = s " $underlying mBTC "
60+ override def toString = s " ${milliBtcFormat.format( underlying)} mBTC "
5661 // @formatter:on
5762}
5863
@@ -82,12 +87,14 @@ case class Btc(private val underlying: BigDecimal) extends BtcAmount with Ordere
8287 def toBigDecimal : BigDecimal = underlying
8388 def toDouble : Double = underlying.toDouble
8489 def toLong : Long = underlying.toLong
85- override def toString = s " $underlying BTC "
90+ override def toString = s " ${btcFormat.format( underlying)} BTC "
8691 // @formatter:on
8792}
8893
8994object BtcAmount {
9095 val Coin = 100000000L
9196 val Cent = 1000000L
9297 val MaxMoney : Double = 21e6 * Coin
93- }
98+ val milliBtcFormat = new DecimalFormat (" #.#####" , new DecimalFormatSymbols (Locale .US ))
99+ val btcFormat = new DecimalFormat (" #.########" , new DecimalFormatSymbols (Locale .US ))
100+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ package fr.acinq.bitcoin.scalacompat
22
33import org .scalatest .FunSuite
44
5+ import java .util .Locale
6+
57class BtcAmountSpec extends FunSuite {
68
79 test(" btc/millibtc/satoshi conversions" ) {
@@ -94,4 +96,23 @@ class BtcAmountSpec extends FunSuite {
9496 assert((1.1 btc).min(90000000 sat) === Btc (0.9 ))
9597 }
9698
99+ test(" toString formatting" ) {
100+ assert((0.00000000 btc).toString == " 0 BTC" )
101+ assert((10.00000000 btc).toString == " 10 BTC" )
102+ assert((1.23456789 btc).toString == " 1.23456789 BTC" )
103+ assert((- 1.23456789 btc).toString == " -1.23456789 BTC" )
104+ assert((1.2345 btc).toString == " 1.2345 BTC" )
105+ assert((- 1.2345 btc).toString == " -1.2345 BTC" )
106+
107+ assert((0 btc).toMilliBtc.toString == " 0 mBTC" )
108+ assert((10.00000000 btc).toMilliBtc.toString == " 10000 mBTC" )
109+ assert((1.23456789 btc).toMilliBtc.toString == " 1234.56789 mBTC" )
110+ assert((- 1.23456789 btc).toMilliBtc.toString == " -1234.56789 mBTC" )
111+ assert((1.2345 btc).toMilliBtc.toString == " 1234.5 mBTC" )
112+ assert((- 1.2345 btc).toMilliBtc.toString == " -1234.5 mBTC" )
113+ assert((1.234 btc).toMilliBtc.toString == " 1234 mBTC" )
114+ assert((- 1.234 btc).toMilliBtc.toString == " -1234 mBTC" )
115+
116+ }
117+
97118}
You can’t perform that action at this time.
0 commit comments