File tree 2 files changed +31
-3
lines changed
main/scala/fr/acinq/bitcoin/scalacompat
test/scala/fr/acinq/bitcoin/scalacompat 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change 1
1
package fr .acinq .bitcoin .scalacompat
2
2
3
+ import fr .acinq .bitcoin .scalacompat .BtcAmount .{btcFormat , milliBtcFormat }
4
+
5
+ import java .text .{DecimalFormat , DecimalFormatSymbols }
6
+ import java .util .Locale
7
+
3
8
sealed trait BtcAmount
4
9
5
10
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
52
57
def toBigDecimal : BigDecimal = underlying
53
58
def toDouble : Double = underlying.toDouble
54
59
def toLong : Long = underlying.toLong
55
- override def toString = s " $underlying mBTC "
60
+ override def toString = s " ${milliBtcFormat.format( underlying)} mBTC "
56
61
// @formatter:on
57
62
}
58
63
@@ -82,12 +87,14 @@ case class Btc(private val underlying: BigDecimal) extends BtcAmount with Ordere
82
87
def toBigDecimal : BigDecimal = underlying
83
88
def toDouble : Double = underlying.toDouble
84
89
def toLong : Long = underlying.toLong
85
- override def toString = s " $underlying BTC "
90
+ override def toString = s " ${btcFormat.format( underlying)} BTC "
86
91
// @formatter:on
87
92
}
88
93
89
94
object BtcAmount {
90
95
val Coin = 100000000L
91
96
val Cent = 1000000L
92
97
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
2
2
3
3
import org .scalatest .FunSuite
4
4
5
+ import java .util .Locale
6
+
5
7
class BtcAmountSpec extends FunSuite {
6
8
7
9
test(" btc/millibtc/satoshi conversions" ) {
@@ -94,4 +96,23 @@ class BtcAmountSpec extends FunSuite {
94
96
assert((1.1 btc).min(90000000 sat) === Btc (0.9 ))
95
97
}
96
98
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
+
97
118
}
You can’t perform that action at this time.
0 commit comments