Skip to content

Commit 7e22f43

Browse files
authored
Use a formatter to display Btc/MilliBtc amounts (#90)
Display at most 8 decimals, instead of the default which may even use the exponent display occasionnally.
1 parent 522ff59 commit 7e22f43

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/main/scala/fr/acinq/bitcoin/scalacompat/BtcAmount.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package 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+
38
sealed trait BtcAmount
49

510
case 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

8994
object 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+
}

src/test/scala/fr/acinq/bitcoin/scalacompat/BtcAmountSpec.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package fr.acinq.bitcoin.scalacompat
22

33
import org.scalatest.FunSuite
44

5+
import java.util.Locale
6+
57
class 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
}

0 commit comments

Comments
 (0)