Package org.bitcoinj.core
Class Coin
java.lang.Object
org.bitcoinj.core.Coin
- All Implemented Interfaces:
Serializable
,Comparable<Coin>
,Monetary
Represents a monetary Bitcoin value. This class is immutable.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Coin
0.01 Bitcoins.static final Coin
One Bitcoin.static final Coin
static final Coin
0.000001 Bitcoins, also known as 1 µBTC or 1 uBTC.static final Coin
0.001 Bitcoins, also known as 1 mBTC.static final Coin
Represents a monetary value of minus one satoshi.static final Coin
A satoshi is the smallest unit that can be transferred.static final int
Number of decimals for one Bitcoin.final long
The number of satoshis of this monetary value.static final Coin
Zero Bitcoins. -
Method Summary
Modifier and TypeMethodDescriptionstatic long
btcToSatoshi
(BigDecimal coins) Convert a decimal amount of BTC into satoshis.int
div
(int divisor) Alias for dividediv
(long divisor) Alias for dividedivide
(long divisor) long
Coin[]
divideAndRemainder
(long divisor) boolean
long
getValue()
Returns the number of satoshis of this monetary value.int
hashCode()
boolean
isGreaterThan
(Coin other) Returns true if the monetary value represented by this instance is greater than that of the given other Coin, otherwise false.boolean
isLessThan
(Coin other) Returns true if the monetary value represented by this instance is less than that of the given other Coin, otherwise false.boolean
Returns true if and only if this instance represents a monetary value less than zero, otherwise false.boolean
Returns true if and only if this instance represents a monetary value greater than zero, otherwise false.boolean
isZero()
Returns true if and only if this instance represents zero monetary value, otherwise false.long
Returns the number of satoshis of this monetary value.Alias for subtractmultiply
(long factor) negate()
static Coin
ofBtc
(BigDecimal coins) Create aCoin
from a decimal amount of BTC.static Coin
ofSat
(long satoshis) Create aCoin
from a long integer number of satoshis.static Coin
Create aCoin
by parsing aString
amount expressed in "the way humans are used to".static Coin
parseCoinInexact
(String str) Create aCoin
by parsing aString
amount expressed in "the way humans are used to".Alias for addstatic BigDecimal
satoshiToBtc
(long satoshis) Convert an amount in satoshis to an amount in BTC.shiftLeft
(int n) shiftRight
(int n) int
signum()
int
Returns the absolute value of exponent of the value of a "smallest unit" in scientific notation.times
(int factor) Alias for multiplytimes
(long factor) Alias for multiplytoBtc()
Convert to number of bitcoin (in BTC)Returns the value as a 0.12 type string.Returns the value as a plain string denominated in BTC.long
toSat()
Convert to number of satoshistoString()
static Coin
valueOf
(int coins, int cents) Create aCoin
from an amount expressed in "the way humans are used to".static Coin
valueOf
(long satoshis) Create aCoin
from a long integer number of satoshis.
-
Field Details
-
SMALLEST_UNIT_EXPONENT
public static final int SMALLEST_UNIT_EXPONENTNumber of decimals for one Bitcoin. This constant is useful for quick adapting to other coins because a lot of constants derive from it.- See Also:
-
ZERO
Zero Bitcoins. -
COIN
One Bitcoin. -
CENT
0.01 Bitcoins. This unit is not really used much. -
MILLICOIN
0.001 Bitcoins, also known as 1 mBTC. -
MICROCOIN
0.000001 Bitcoins, also known as 1 µBTC or 1 uBTC. -
SATOSHI
A satoshi is the smallest unit that can be transferred. 100 million of them fit into a Bitcoin. -
FIFTY_COINS
-
NEGATIVE_SATOSHI
Represents a monetary value of minus one satoshi. -
value
public final long valueThe number of satoshis of this monetary value.
-
-
Method Details
-
valueOf
Create aCoin
from a long integer number of satoshis.- Parameters:
satoshis
- number of satoshis- Returns:
Coin
object containing value in satoshis
-
smallestUnitExponent
public int smallestUnitExponent()Description copied from interface:Monetary
Returns the absolute value of exponent of the value of a "smallest unit" in scientific notation. For Bitcoin, a satoshi is worth 1E-8 so this would be 8.- Specified by:
smallestUnitExponent
in interfaceMonetary
-
getValue
public long getValue()Returns the number of satoshis of this monetary value. -
valueOf
Create aCoin
from an amount expressed in "the way humans are used to".- Parameters:
coins
- Number of bitcoinscents
- Number of bitcents (0.01 bitcoin)- Returns:
Coin
object containing value in satoshis
-
btcToSatoshi
Convert a decimal amount of BTC into satoshis.- Parameters:
coins
- number of coins- Returns:
- number of satoshis
-
satoshiToBtc
Convert an amount in satoshis to an amount in BTC.- Parameters:
satoshis
- number of satoshis- Returns:
- number of bitcoins (in BTC)
-
ofBtc
Create aCoin
from a decimal amount of BTC.- Parameters:
coins
- number of coins (in BTC)- Returns:
Coin
object containing value in satoshis
-
ofSat
Create aCoin
from a long integer number of satoshis.- Parameters:
satoshis
- number of satoshis- Returns:
Coin
object containing value in satoshis
-
parseCoin
Create aCoin
by parsing aString
amount expressed in "the way humans are used to".- Parameters:
str
- string in a format understood byBigDecimal(String)
, for example "0", "1", "0.10", * "1.23E3", "1234.5E-5".- Returns:
Coin
object containing value in satoshis- Throws:
IllegalArgumentException
- if you try to specify fractional satoshis, or a value out of range.
-
parseCoinInexact
Create aCoin
by parsing aString
amount expressed in "the way humans are used to". The amount is cut to satoshi precision.- Parameters:
str
- string in a format understood byBigDecimal(String)
, for example "0", "1", "0.10", * "1.23E3", "1234.5E-5".- Returns:
Coin
object containing value in satoshis- Throws:
IllegalArgumentException
- if you try to specify a value out of range.
-
add
-
plus
Alias for add -
subtract
-
minus
Alias for subtract -
multiply
-
times
Alias for multiply -
times
Alias for multiply -
divide
-
div
Alias for divide -
div
Alias for divide -
divideAndRemainder
-
divide
-
isPositive
public boolean isPositive()Returns true if and only if this instance represents a monetary value greater than zero, otherwise false. -
isNegative
public boolean isNegative()Returns true if and only if this instance represents a monetary value less than zero, otherwise false. -
isZero
public boolean isZero()Returns true if and only if this instance represents zero monetary value, otherwise false. -
isGreaterThan
Returns true if the monetary value represented by this instance is greater than that of the given other Coin, otherwise false. -
isLessThan
Returns true if the monetary value represented by this instance is less than that of the given other Coin, otherwise false. -
shiftLeft
-
shiftRight
-
signum
public int signum() -
negate
-
longValue
public long longValue()Returns the number of satoshis of this monetary value. It's deprecated in favour of accessingvalue
directly. -
toSat
public long toSat()Convert to number of satoshis- Returns:
- decimal number of satoshis
-
toBtc
Convert to number of bitcoin (in BTC)- Returns:
- decimal number of bitcoin (in BTC)
-
toFriendlyString
Returns the value as a 0.12 type string. More digits after the decimal place will be used if necessary, but two will always be present. -
toPlainString
Returns the value as a plain string denominated in BTC. The result is unformatted with no trailing zeroes. For instance, a value of 150000 satoshis gives an output string of "0.0015" BTC
-
toString
-
equals
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<Coin>
-