本文整理汇总了Java中java.math.BigDecimal.precision方法的典型用法代码示例。如果您正苦于以下问题:Java BigDecimal.precision方法的具体用法?Java BigDecimal.precision怎么用?Java BigDecimal.precision使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.precision方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculate
import java.math.BigDecimal; //导入方法依赖的package包/类
private static BigDecimal calculate(String input) {
int i = 1;
while (i < input.length() && !order.containsKey(input.charAt(i)))
i++;
String var = input.substring(0, i);
BigDecimal value = variables.getOrDefault(var, new BigDecimal(var));
char ope = i < input.length() ? input.charAt(0) : 0;
if (ope == 0) return value;
int currOrder = order.getOrDefault(ope, -1);
BigDecimal result = null;
if (currOrder > 0)
result = calculate(input.substring(i + 1));
switch (currOrder) {
case '+':
return value.add(result);
case '-':
return value.subtract(result);
case '*':
return value.multiply(result);
case '/':
return value.divide(result, BigDecimal.ROUND_HALF_UP);
case '^':
// If pow of int BigDecimal can handle it
if (result.precision() == 0) return value.pow(result.intValue());
// Otherwise we gotta revert to power of doubles
return BigDecimal.valueOf(Math.pow(value.doubleValue(), result.doubleValue()));
}
return null;
}
示例2: deserializeBigDecimal
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Deserialize a Volt fixed precision and scale 16-byte decimal and return
* it as a {@link java.math.BigDecimal BigDecimal} .
* @param in {@link org.voltdb.messaging.FastDeserializer FastDeserializer} to read from
* @throws IOException Thrown by <code>FastDeserializer</code>
*/
public static BigDecimal deserializeBigDecimal(FastDeserializer in)
throws IOException
{
byte decimalBytes[] = new byte[16];
in.readFully(decimalBytes);
if (java.util.Arrays.equals(decimalBytes, NULL_INDICATOR)) {
return null;
}
final BigDecimal bd = new BigDecimal(new BigInteger(decimalBytes),
kDefaultScale, context);
if (bd.precision() > 38) {
throw new IOException("Decimal " + bd + " has precision > 38.");
}
return bd;
}
示例3: precision
import java.math.BigDecimal; //导入方法依赖的package包/类
public static int precision(BigDecimal o) {
if (o == null) {
return 0;
}
//#ifdef JAVA6
return o.precision();
//#else
/*
BigInteger big = o.unscaledValue();
int sign = big.signum() == -1 ? 1
: 0;
return big.toString().length() - sign;
*/
//#endif JAVA6
}
示例4: encodeBinary
import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public final void encodeBinary(final BigDecimal value, final BufferWriter b) {
final BigDecimal minimized = value.stripTrailingZeros();
final BigInteger unscaled = minimized.abs().unscaledValue();
final int sign = minimized.signum();
final int beforeDecimal = minimized.precision() - minimized.scale();
// the decimal point must align on a base-10000 digit
final int padZeroes = 4 - minimized.scale() % 4;
BigInteger paddedUnscaled;
if (padZeroes == 0)
paddedUnscaled = unscaled;
else
paddedUnscaled = unscaled.multiply(BigInteger.valueOf(10).pow(padZeroes));
final short[] digits = findDigits(paddedUnscaled, EMPTY_SHORT_ARRAY);
int weight;
if (digits.length != 0) {
int firstDigitSize;
if (digits[0] < 10)
firstDigitSize = 1;
else if (digits[0] < 100)
firstDigitSize = 2;
else if (digits[0] < 1000)
firstDigitSize = 3;
else
firstDigitSize = 4;
weight = (beforeDecimal - firstDigitSize) / 4;
} else
weight = 0;
b.writeShort((short) digits.length);
b.writeShort((short) weight);
b.writeShort(sign < 0 ? NUMERIC_NEG : NUMERIC_POS);
b.writeShort((short) value.scale());
for (final short digit : digits)
b.writeShort(digit);
}
示例5: deserializeBigDecimalFromString
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Deserialize a Volt fixed precision and scale 16-byte decimal from a String representation
* @param decimal <code>String</code> representation of the decimal
*/
public static BigDecimal deserializeBigDecimalFromString(String decimal) throws IOException
{
if (decimal == null) {
return null;
}
final BigDecimal bd = new BigDecimal(decimal);
if (bd.scale() > 12) {
throw new IOException("Decimal " + bd + " has more then 12 digits of scale");
}
if (bd.precision() > 38) {
throw new RuntimeException("Decimal " + bd + " has more than 38 digits of precision.");
}
return bd;
}
示例6: getDecimal28Holder
import java.math.BigDecimal; //导入方法依赖的package包/类
public static Decimal28SparseHolder getDecimal28Holder(DrillBuf buf, String decimal) {
Decimal28SparseHolder dch = new Decimal28SparseHolder();
BigDecimal bigDecimal = new BigDecimal(decimal);
dch.scale = bigDecimal.scale();
dch.precision = bigDecimal.precision();
Decimal28SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer);
dch.start = 0;
dch.buffer = buf.reallocIfNeeded(5 * DecimalUtility.integerSize);
DecimalUtility.getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, dch.precision, dch.nDecimalDigits);
return dch;
}
示例7: getDecimal38Holder
import java.math.BigDecimal; //导入方法依赖的package包/类
public static Decimal38SparseHolder getDecimal38Holder(DrillBuf buf, String decimal) {
Decimal38SparseHolder dch = new Decimal38SparseHolder();
BigDecimal bigDecimal = new BigDecimal(decimal);
dch.scale = bigDecimal.scale();
dch.precision = bigDecimal.precision();
Decimal38SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer);
dch.start = 0;
dch.buffer = buf.reallocIfNeeded(dch.maxPrecision * DecimalUtility.integerSize);
DecimalUtility.getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, dch.precision, dch.nDecimalDigits);
return dch;
}
示例8: next
import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public BigDecimal next() {
BigInteger unscaled = new BigInteger(precision, rng);
BigDecimal unscaledBD = new BigDecimal(unscaled);
int scale =
rng.nextInt(MAX_SCALE - MIN_SCALE + 1) + MIN_SCALE
- unscaledBD.precision();
BigDecimal result = new BigDecimal(unscaled, -scale);
if (rng.nextBoolean()) {
result = result.negate();
}
return result;
}
示例9: getScaledValue
import java.math.BigDecimal; //导入方法依赖的package包/类
private String getScaledValue(String valueStr, long decimals) {
// Perform decimal conversion
BigDecimal value = new BigDecimal(valueStr);
value = value.divide(new BigDecimal(Math.pow(10, decimals)));
int scale = 3 - value.precision() + value.scale();
return value.setScale(scale, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}
示例10: parse
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Parses a price of type <code>String</code> and converts it to a
* <code>BigDecimal</code>, considering the active locale. It accepts
* maximum {@link #NUMBER_OF_INTEGER_PLACES} integer digits and
* {@link #NUMBER_OF_DECIMAL_PLACES} decimal digits. It accepts grouping
* separators according to the active locale, placed at any position in the
* integer part, but not in the decimal part. It accepts no <code>+</code>
* symbol and no currency symbols (like <code>€</code> or <code>$</code>).
* Also, If <code>allosNegativePrice</code> is <code>true</code>, negative
* price is not accepted.
*
* @param price
* The price to be parsed as <code>String</code>. May be
* <code>null</code>, otherwise it will be trimmed.
* @param allosNegativePrice
* Defined if negative price value is allowed or not. If value is
* <code>true</code>, negative price is not accepted.
* @return The parsed <code>BigDecimal</code> representing the
* <code>price</code>. Returns <code>BigDecimal.ZERO</code> if the
* given <code>price</code> parameter is <code>null</code> or it
* contains no characters other then blanks.
* @throws ParseException
* if the specified <code>price</code> string represents no
* valid price as described.
*/
public BigDecimal parse(String price, boolean allowNegativePrice)
throws ParseException {
if (price == null || price.trim().length() == 0) {
return BigDecimal.ZERO;
}
BigDecimal bigDecimal = VALIDATOR.validate(price,
FORMAT_PATTERN_WITH_GROUPING, getActiveLocale());
if (bigDecimal == null) {
throw new ParseException("Invalid price: " + price, 0);
} else if (bigDecimal.compareTo(BigDecimal.ZERO) < 0
&& !allowNegativePrice) {
// used compareTo() to ignore the scale
throw new ParseException("Negative price: " + price, 0);
}
if (bigDecimal.scale() > NUMBER_OF_DECIMAL_PLACES
|| (bigDecimal.precision() - bigDecimal.scale()) > NUMBER_OF_INTEGER_PLACES) {
throw new ParseException(
"ERROR_PRICEMODEL_INVALID_FRACTIONAL_PART", 0);
}
return bigDecimal;
}
示例11: Decimal9Expression
import java.math.BigDecimal; //导入方法依赖的package包/类
public Decimal9Expression(BigDecimal input, ExpressionPosition pos) {
super(pos);
this.scale = input.scale();
this.precision = input.precision();
this.decimal = CoreDecimalUtility.getDecimal9FromBigDecimal(input, scale, precision);
}
示例12: Decimal18Expression
import java.math.BigDecimal; //导入方法依赖的package包/类
public Decimal18Expression(BigDecimal input, ExpressionPosition pos) {
super(pos);
this.scale = input.scale();
this.precision = input.precision();
this.decimal = CoreDecimalUtility.getDecimal18FromBigDecimal(input, scale, precision);
}
示例13: weiToEth
import java.math.BigDecimal; //导入方法依赖的package包/类
public static BigDecimal weiToEth(BigInteger wei, int sigFig) throws Exception {
BigDecimal eth = weiToEth(wei);
int scale = sigFig - eth.precision() + eth.scale();
return eth.setScale(scale, RoundingMode.HALF_UP);
}
示例14: DecimalExpression
import java.math.BigDecimal; //导入方法依赖的package包/类
public DecimalExpression(BigDecimal input) {
this.scale = input.scale();
this.precision = input.precision();
this.decimal = CoreDecimalUtility.getDecimal9FromBigDecimal(input, scale, precision);
}