本文整理汇总了Java中java.math.BigDecimal.movePointRight方法的典型用法代码示例。如果您正苦于以下问题:Java BigDecimal.movePointRight方法的具体用法?Java BigDecimal.movePointRight怎么用?Java BigDecimal.movePointRight使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.movePointRight方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseObject
import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public Number parseObject(String source, ParsePosition pos) {
Number number = NumberFormat.getInstance().parse(source, pos);
if (number == null) {
return null;
}
String unit = source.substring(pos.getIndex()).trim();
Integer exponent = null;
if (!unit.isEmpty()) {
String prefix = unit.substring(0, 1);
exponent = PREFIX_MAP.get(prefix);
}
if (exponent != null && Double.isFinite(number.doubleValue())) {
BigDecimal bd = new BigDecimal(number.toString());
bd = bd.movePointRight(exponent.intValue());
if (bd.remainder(BigDecimal.ONE).equals(BigDecimal.ZERO) &&
bd.abs().compareTo(new BigDecimal(Long.MAX_VALUE)) < 0) {
return bd.longValue();
}
return bd.doubleValue();
}
return number;
}
示例2: setTarget
import java.math.BigDecimal; //导入方法依赖的package包/类
public void setTarget(String value) {
BigDecimal num = new BigDecimal(value);
num = num.movePointRight(2);
jdkTarget = num.intValue();
}
示例3: scale
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Calculate the appropriate denomination for the given Bitcoin monetary value. This
* method takes a BigInteger representing a quantity of satoshis, and returns the
* number of places that value's decimal point is to be moved when formatting said value
* in order that the resulting number represents the correct quantity of denominational
* units.
*
* <p>As a side-effect, this sets the units indicators of the underlying NumberFormat object.
* Only invoke this from a synchronized method, and be sure to put the DecimalFormatSymbols
* back to its proper state, otherwise immutability, equals() and hashCode() fail.
*/
@Override
protected int scale(BigInteger satoshis, int fractionPlaces) {
/* The algorithm is as follows. TODO: is there a way to optimize step 4?
1. Can we use coin denomination w/ no rounding? If yes, do it.
2. Else, can we use millicoin denomination w/ no rounding? If yes, do it.
3. Else, can we use micro denomination w/ no rounding? If yes, do it.
4. Otherwise we must round:
(a) round to nearest coin + decimals
(b) round to nearest millicoin + decimals
(c) round to nearest microcoin + decimals
Subtract each of (a), (b) and (c) from the true value, and choose the
denomination that gives smallest absolute difference. It case of tie, use the
smaller denomination.
*/
int places;
int coinOffset = Math.max(SMALLEST_UNIT_EXPONENT - fractionPlaces, 0);
BigDecimal inCoins = new BigDecimal(satoshis).movePointLeft(coinOffset);
if (inCoins.remainder(ONE).compareTo(ZERO) == 0) {
places = COIN_SCALE;
} else {
BigDecimal inMillis = inCoins.movePointRight(MILLICOIN_SCALE);
if (inMillis.remainder(ONE).compareTo(ZERO) == 0) {
places = MILLICOIN_SCALE;
} else {
BigDecimal inMicros = inCoins.movePointRight(MICROCOIN_SCALE);
if (inMicros.remainder(ONE).compareTo(ZERO) == 0) {
places = MICROCOIN_SCALE;
} else {
// no way to avoid rounding: so what denomination gives smallest error?
BigDecimal a = inCoins.subtract(inCoins.setScale(0, HALF_UP)).
movePointRight(coinOffset).abs();
BigDecimal b = inMillis.subtract(inMillis.setScale(0, HALF_UP)).
movePointRight(coinOffset - MILLICOIN_SCALE).abs();
BigDecimal c = inMicros.subtract(inMicros.setScale(0, HALF_UP)).
movePointRight(coinOffset - MICROCOIN_SCALE).abs();
if (a.compareTo(b) < 0)
if (a.compareTo(c) < 0) places = COIN_SCALE;
else places = MICROCOIN_SCALE;
else if (b.compareTo(c) < 0) places = MILLICOIN_SCALE;
else places = MICROCOIN_SCALE;
}
}
}
prefixUnitsIndicator(numberFormat, places);
return places;
}