Class Coin
- java.lang.Object
 - 
- org.bitcoinj.base.Coin
 
 
- 
public final class Coin extends java.lang.Object implements Monetary, java.lang.Comparable<Coin>
Represents a monetary Bitcoin value. This class is immutable and should be treated as a Java Value-based class. We recommend using theCoinclass wherever possible to represent Bitcoin monetary values. If you have existing code that uses other numeric types and need to convert there are conversion methods.Internally
Coinis implemented as along(seevalue) that represents a number of satoshis. It can also be considered a fixed-point number of bitcoins.To create a
Coinfrom an integer number of satoshis, useofSat(long). To convert to alongnumber of satoshis usetoSat(). (You can also usevalueOf(long),getValue()orvalue.)To create a
Coinfrom a decimal number of bitcoins, useofBtc(BigDecimal). To convert to aBigDecimalof bitcoins usetoBtc(). (Performing fixed-point conversion, these methods essentially multiply or divide byCoin.COIN.toSat().)Never ever use
floatordoubleto represent monetary values. 
- 
- 
Field Summary
Fields Modifier and Type Field Description static intBYTESNumber of bytes to store this amount.static CoinCENT0.01 Bitcoins.static CoinCOINOne Bitcoin.static CoinFIFTY_COINSstatic CoinMICROCOIN0.000001 Bitcoins, also known as 1 µBTC or 1 uBTC.static CoinMILLICOIN0.001 Bitcoins, also known as 1 mBTC.static CoinNEGATIVE_SATOSHIRepresents a monetary value of minus one satoshi.static CoinSATOSHIA satoshi is the smallest unit that can be transferred.static intSMALLEST_UNIT_EXPONENTNumber of decimals for one Bitcoin.longvalueThe number of satoshis of this monetary value.static CoinZEROZero Bitcoins. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Coinadd(Coin value)static longbtcToSatoshi(java.math.BigDecimal coins)Convert a decimal amount of BTC into satoshis.intcompareTo(Coin other)Coindiv(int divisor)Alias for divideCoindiv(long divisor)Alias for divideCoindivide(long divisor)longdivide(Coin divisor)Coin[]divideAndRemainder(long divisor)booleanequals(java.lang.Object o)longgetValue()Returns the number of satoshis of this monetary value.inthashCode()booleanisGreaterThan(Coin other)Returns true if the monetary value represented by this instance is greater than that of the given other Coin, otherwise false.booleanisLessThan(Coin other)Returns true if the monetary value represented by this instance is less than that of the given other Coin, otherwise false.booleanisNegative()Returns true if and only if this instance represents a monetary value less than zero, otherwise false.booleanisPositive()Returns true if and only if this instance represents a monetary value greater than zero, otherwise false.booleanisZero()Returns true if and only if this instance represents zero monetary value, otherwise false.longlongValue()Returns the number of satoshis of this monetary value.Coinminus(Coin value)Alias for subtractCoinmultiply(long factor)Coinnegate()static CoinofBtc(java.math.BigDecimal coins)Create aCoinfrom a decimal amount of BTC.static CoinofSat(long satoshis)Create aCoinfrom a long integer number of satoshis.static CoinparseCoin(java.lang.String str)Create aCoinby parsing aStringamount expressed in "the way humans are used to".static CoinparseCoinInexact(java.lang.String str)Create aCoinby parsing aStringamount expressed in "the way humans are used to".Coinplus(Coin value)Alias for addstatic Coinread(java.nio.ByteBuffer buf)Read a Coin amount from the given buffer as 8 bytes in little-endian order.static java.math.BigDecimalsatoshiToBtc(long satoshis)Convert an amount in satoshis to an amount in BTC.byte[]serialize()Allocates a byte array and serializes the amount.CoinshiftLeft(int n)CoinshiftRight(int n)intsignum()intsmallestUnitExponent()Returns the absolute value of exponent of the value of a "smallest unit" in scientific notation.Coinsubtract(Coin value)Cointimes(int factor)Alias for multiplyCointimes(long factor)Alias for multiplyjava.math.BigDecimaltoBtc()Convert to number of bitcoin (in BTC)java.lang.StringtoFriendlyString()Returns the value as a 0.12 type string.java.lang.StringtoPlainString()Returns the value as a plain string denominated in BTC.longtoSat()Convert to number of satoshisjava.lang.StringtoString()static CoinvalueOf(int coins, int cents)Create aCoinfrom an amount expressed in "the way humans are used to".static CoinvalueOf(long satoshis)Create aCoinfrom a long integer number of satoshis.java.nio.ByteBufferwrite(java.nio.ByteBuffer buf)Write the amount into the given buffer as 8 bytes in little-endian order. 
 - 
 
- 
- 
Field Detail
- 
SMALLEST_UNIT_EXPONENT
public static final int SMALLEST_UNIT_EXPONENT
Number 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:
 - Constant Field Values
 
 
- 
ZERO
public static final Coin ZERO
Zero Bitcoins. 
- 
COIN
public static final Coin COIN
One Bitcoin. 
- 
CENT
public static final Coin CENT
0.01 Bitcoins. This unit is not really used much. 
- 
MILLICOIN
public static final Coin MILLICOIN
0.001 Bitcoins, also known as 1 mBTC. 
- 
MICROCOIN
public static final Coin MICROCOIN
0.000001 Bitcoins, also known as 1 µBTC or 1 uBTC. 
- 
SATOSHI
public static final Coin SATOSHI
A satoshi is the smallest unit that can be transferred. 100 million of them fit into a Bitcoin. 
- 
FIFTY_COINS
public static final Coin FIFTY_COINS
 
- 
NEGATIVE_SATOSHI
public static final Coin NEGATIVE_SATOSHI
Represents a monetary value of minus one satoshi. 
- 
BYTES
public static final int BYTES
Number of bytes to store this amount.- See Also:
 - Constant Field Values
 
 
- 
value
public final long value
The number of satoshis of this monetary value. 
 - 
 
- 
Method Detail
- 
valueOf
public static Coin valueOf(long satoshis)
Create aCoinfrom a long integer number of satoshis.- Parameters:
 satoshis- number of satoshis- Returns:
 Coinobject containing value in satoshis
 
- 
read
public static Coin read(java.nio.ByteBuffer buf) throws java.nio.BufferUnderflowException
Read a Coin amount from the given buffer as 8 bytes in little-endian order.- Parameters:
 buf- buffer to read from- Returns:
 - read amount
 - Throws:
 java.nio.BufferUnderflowException- if the read value extends beyond the remaining bytes of the buffer
 
- 
smallestUnitExponent
public int smallestUnitExponent()
Description copied from interface:MonetaryReturns 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:
 smallestUnitExponentin interfaceMonetary
 
- 
getValue
public long getValue()
Returns the number of satoshis of this monetary value. 
- 
valueOf
public static Coin valueOf(int coins, int cents)
Create aCoinfrom an amount expressed in "the way humans are used to".- Parameters:
 coins- Number of bitcoinscents- Number of bitcents (0.01 bitcoin)- Returns:
 Coinobject containing value in satoshis
 
- 
btcToSatoshi
public static long btcToSatoshi(java.math.BigDecimal coins) throws java.lang.ArithmeticExceptionConvert a decimal amount of BTC into satoshis.- Parameters:
 coins- number of coins- Returns:
 - number of satoshis
 - Throws:
 java.lang.ArithmeticException- if value has too much precision or will not fit in a long
 
- 
satoshiToBtc
public static java.math.BigDecimal satoshiToBtc(long satoshis)
Convert an amount in satoshis to an amount in BTC.- Parameters:
 satoshis- number of satoshis- Returns:
 - number of bitcoins (in BTC)
 
 
- 
ofBtc
public static Coin ofBtc(java.math.BigDecimal coins) throws java.lang.ArithmeticException
Create aCoinfrom a decimal amount of BTC.- Parameters:
 coins- number of coins (in BTC)- Returns:
 Coinobject containing value in satoshis- Throws:
 java.lang.ArithmeticException- if value has too much precision or will not fit in a long
 
- 
ofSat
public static Coin ofSat(long satoshis)
Create aCoinfrom a long integer number of satoshis.- Parameters:
 satoshis- number of satoshis- Returns:
 Coinobject containing value in satoshis
 
- 
parseCoin
public static Coin parseCoin(java.lang.String str)
Create aCoinby parsing aStringamount 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:
 Coinobject containing value in satoshis- Throws:
 java.lang.IllegalArgumentException- if you try to specify fractional satoshis, or a value out of range.
 
- 
parseCoinInexact
public static Coin parseCoinInexact(java.lang.String str)
Create aCoinby parsing aStringamount 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:
 Coinobject containing value in satoshis- Throws:
 java.lang.IllegalArgumentException- if you try to specify a value out of range.
 
- 
multiply
public Coin multiply(long factor)
 
- 
times
public Coin times(long factor)
Alias for multiply 
- 
times
public Coin times(int factor)
Alias for multiply 
- 
divide
public Coin divide(long divisor)
 
- 
div
public Coin div(long divisor)
Alias for divide 
- 
div
public Coin div(int divisor)
Alias for divide 
- 
divideAndRemainder
public Coin[] divideAndRemainder(long divisor)
 
- 
divide
public long divide(Coin divisor)
 
- 
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
public 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. 
- 
isLessThan
public 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. 
- 
shiftLeft
public Coin shiftLeft(int n)
 
- 
shiftRight
public Coin shiftRight(int n)
 
- 
negate
public Coin negate()
 
- 
longValue
public long longValue()
Returns the number of satoshis of this monetary value. It's deprecated in favour of accessingvaluedirectly. 
- 
toSat
public long toSat()
Convert to number of satoshis- Returns:
 - decimal number of satoshis
 
 
- 
toBtc
public java.math.BigDecimal toBtc()
Convert to number of bitcoin (in BTC)- Returns:
 - decimal number of bitcoin (in BTC)
 
 
- 
write
public java.nio.ByteBuffer write(java.nio.ByteBuffer buf) throws java.nio.BufferOverflowExceptionWrite the amount into the given buffer as 8 bytes in little-endian order.- Parameters:
 buf- buffer to write into- Returns:
 - the buffer
 - Throws:
 java.nio.BufferOverflowException- if the value doesn't fit the remaining buffer
 
- 
serialize
public byte[] serialize()
Allocates a byte array and serializes the amount.- Returns:
 - serialized amount
 
 
- 
toFriendlyString
public java.lang.String 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
public java.lang.String 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
public java.lang.String toString()
- Overrides:
 toStringin classjava.lang.Object
 
- 
equals
public boolean equals(java.lang.Object o)
- Overrides:
 equalsin classjava.lang.Object
 
- 
hashCode
public int hashCode()
- Overrides:
 hashCodein classjava.lang.Object
 
 - 
 
 -