Skip to content

Commit

Permalink
Use a formatter to display Btc/MilliBtc amounts (#90)
Browse files Browse the repository at this point in the history
Display at most 8 decimals, instead of the default which may even use the exponent display occasionnally.
  • Loading branch information
pm47 authored Feb 12, 2025
1 parent 522ff59 commit 7e22f43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/scala/fr/acinq/bitcoin/scalacompat/BtcAmount.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package fr.acinq.bitcoin.scalacompat

import fr.acinq.bitcoin.scalacompat.BtcAmount.{btcFormat, milliBtcFormat}

import java.text.{DecimalFormat, DecimalFormatSymbols}
import java.util.Locale

sealed trait BtcAmount

case class Satoshi(private val underlying: Long) extends BtcAmount with Ordered[Satoshi] {
Expand Down Expand Up @@ -52,7 +57,7 @@ case class MilliBtc(private val underlying: BigDecimal) extends BtcAmount with O
def toBigDecimal: BigDecimal = underlying
def toDouble: Double = underlying.toDouble
def toLong: Long = underlying.toLong
override def toString = s"$underlying mBTC"
override def toString = s"${milliBtcFormat.format(underlying)} mBTC"
// @formatter:on
}

Expand Down Expand Up @@ -82,12 +87,14 @@ case class Btc(private val underlying: BigDecimal) extends BtcAmount with Ordere
def toBigDecimal: BigDecimal = underlying
def toDouble: Double = underlying.toDouble
def toLong: Long = underlying.toLong
override def toString = s"$underlying BTC"
override def toString = s"${btcFormat.format(underlying)} BTC"
// @formatter:on
}

object BtcAmount {
val Coin = 100000000L
val Cent = 1000000L
val MaxMoney: Double = 21e6 * Coin
}
val milliBtcFormat = new DecimalFormat("#.#####", new DecimalFormatSymbols(Locale.US))
val btcFormat = new DecimalFormat("#.########", new DecimalFormatSymbols(Locale.US))
}
21 changes: 21 additions & 0 deletions src/test/scala/fr/acinq/bitcoin/scalacompat/BtcAmountSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fr.acinq.bitcoin.scalacompat

import org.scalatest.FunSuite

import java.util.Locale

class BtcAmountSpec extends FunSuite {

test("btc/millibtc/satoshi conversions") {
Expand Down Expand Up @@ -94,4 +96,23 @@ class BtcAmountSpec extends FunSuite {
assert((1.1 btc).min(90000000 sat) === Btc(0.9))
}

test("toString formatting") {
assert((0.00000000 btc).toString == "0 BTC")
assert((10.00000000 btc).toString == "10 BTC")
assert((1.23456789 btc).toString == "1.23456789 BTC")
assert((-1.23456789 btc).toString == "-1.23456789 BTC")
assert((1.2345 btc).toString == "1.2345 BTC")
assert((-1.2345 btc).toString == "-1.2345 BTC")

assert((0 btc).toMilliBtc.toString == "0 mBTC")
assert((10.00000000 btc).toMilliBtc.toString == "10000 mBTC")
assert((1.23456789 btc).toMilliBtc.toString == "1234.56789 mBTC")
assert((-1.23456789 btc).toMilliBtc.toString == "-1234.56789 mBTC")
assert((1.2345 btc).toMilliBtc.toString == "1234.5 mBTC")
assert((-1.2345 btc).toMilliBtc.toString == "-1234.5 mBTC")
assert((1.234 btc).toMilliBtc.toString == "1234 mBTC")
assert((-1.234 btc).toMilliBtc.toString == "-1234 mBTC")

}

}

0 comments on commit 7e22f43

Please sign in to comment.