public static final BigInteger ZERO = new BigInteger(new int[0], 0);
public static final BigInteger ONE = valueOf(1);
public static final BigInteger TEN = valueOf(10);
常量包括0,1,10。
1.3 构造器
public BigInteger(byte[] val)
public BigInteger(int signum, byte[] magnitude)
public BigInteger(String val, int radix)
public BigInteger(String val)
public BigInteger(int numBits, Random rnd)
public BigInteger(int bitLength, int certainty, Random rnd)
(1) public BigInteger(byte[] val)
将一个包含二进制补码的字节数组转换成BigInteger,如果第一个字节是负数,则这个byte[] val就是负数的补码。因此通过补码的逆运算(补码的补码)可以得到负数的绝对值,再将符号位设置为-,则得到这个补码所代表的负数。
public BigDecimal divide(BigDecimal divisor, int roundingMode)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
scale为小数位数,roundingMode为小数模式:
public static Gas convert(BigInteger gasValue) {
if (Requires.isNull(gasValue))
return null;
BigDecimal value = BigDecimal.ZERO;
BigDecimal gasValueDecimal = new BigDecimal(gasValue);
String unit = "";
BigDecimal divider = new BigDecimal(String.valueOf(FACTOR));
for (int index = 0, len = GAS_UNITS.length; index < len; ++index) {
if (gasValueDecimal.compareTo(divider) < 0 || index == len - 1) {
value = gasValueDecimal;
unit = GAS_UNITS[index];
break;
}
gasValueDecimal = gasValueDecimal.divide(divider, BigDecimal.ROUND_UP);
}
return new Gas(value, unit);
}
public static BigDecimal convert(BigInteger gasValue, String unit) {
if (Requires.isNull(gasValue, unit))
return BigDecimal.ZERO;
int index = getIndex(unit);
if (index == 0)
return new BigDecimal(gasValue);
else if (index > 0)
return new BigDecimal(gasValue).divide(BigDecimal.TEN.pow(index * 3), BigDecimal.ROUND_UP);
throw new UnsupportedOperationException("unsupport unit");
}
public static BigInteger restore(BigDecimal value, String unit) {
if (Requires.isNull(unit))
return BigInteger.ZERO;
int index = getIndex(unit);
if (index == 0)
return value.toBigInteger();
else if (index > 0)
return value.multiply(BigDecimal.TEN.pow(index * 3)).toBigInteger();
throw new UnsupportedOperationException("unsupport unit");
}
public static BigDecimal convertToCTXC(BigInteger gasValue) {
if (Requires.isNull(gasValue))
return BigDecimal.ZERO;
return new BigDecimal(gasValue).divide(BigDecimal.TEN.pow((GAS_UNITS.length - 1) * 3), BigDecimal.ROUND_UP);
}
private static int getIndex(String unit) {
int index = -1;
for (int i = 0, len = GAS_UNITS.length; i < len; i++) {
if (GAS_UNITS.equals(unit)) {
index = i;
break;
}
}
return index;
}
}
上面包含手续费的各种转换操作。
private static BigInteger decodeQuantity(String value) {
if (!isValidHexQuantity(value)) {
throw new MessageDecodingException("Value must be in format 0x[1-9]+[0-9]* or 0x0");
}
try {
return new BigInteger(value.substring(2), 16);
} catch (NumberFormatException e) {
throw new MessageDecodingException("Negative ", e);
}
}
public static BigInteger toBigInteger(String value) {
if (value.startsWith(HEX_PREFIX))
return decodeQuantity(value);
else
return new BigInteger(value);
}
public static BigDecimal toBigDecimal(BigDecimal value, int decimals) {
return value.divide(BigDecimal.TEN.pow(decimals), BigDecimal.ROUND_UP);
}
public static BigDecimal toBigDecimal(BigInteger value, int decimals) {
return toBigDecimal(new BigDecimal(value), decimals);
}
public static BigInteger toBigInteger(BigInteger value, int decimals) {
return value.multiply(BigInteger.TEN.pow(decimals));
}
public static BigInteger toBigInteger(String value, int decimals) {
return new BigDecimal(value).multiply(BigDecimal.TEN.pow(decimals)).toBigInteger();
}
public static String toDecimalString(BigDecimal value, int decimal) {
return toDecimalString(value.toPlainString(), decimal);
}
public static String toDecimalString(String value, int decimal) {
if (value.contains(".")) {
int offset = value.indexOf(".");
int decimalLen = value.length() - 1 - offset;
if (decimalLen > decimal)
value = value.substring(0, offset + (decimal + 1));
value = deleteTailZeroChars(value);
}
return value;
}
public static String deleteTailZeroChars(String value) {
if (TextUtils.isEmpty(value))
return "";
String[] splits = StringUtils.fastSplit(value, '.');
if (splits != null && splits.length == 2) {
int notZeroIndex = -1;
int end = splits[1].length() - 1;
for (int index = end; index >= 0; index--) {
if (splits[1].charAt(index) != '0') {
notZeroIndex = index;
break;
}
}
if (notZeroIndex >= 0) {
if (notZeroIndex == end)
return value;
else {
return new StringBuilder(splits[0]).append(".")
.append(splits[1].substring(0, notZeroIndex + 1)).toString();
}
} else {
return splits[0];
}
}
return value;
}
}