Skip to content

Use a formatter to display Btc/MilliBtc amounts #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")

}

}