本文整理汇总了Java中java.math.BigDecimal.longValueExact方法的典型用法代码示例。如果您正苦于以下问题:Java BigDecimal.longValueExact方法的具体用法?Java BigDecimal.longValueExact怎么用?Java BigDecimal.longValueExact使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.longValueExact方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseSize
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Parses size in human readable format. It's case insensitive
* and accepts values like
* 10G = 10 * 1000 * 1000 * 1000
* 10GB = 10 * 1000 * 1000 * 1000
* 10GiB = 10 * 1024 * 1024 * 1024
* 10 = 10
* 10Mb = 10 * 1000 * 1000
* 10Mib = 10 * 1024 * 1024
* 1kiB = 1 * 1024
*
* @param value human readable size value
* @param defaultValue default value used if {@code null} or non-parsable
* @param overflowValue default value if size is too big
* @return size in {@code long}.
*/
public static long parseSize(String value, long defaultValue, long overflowValue) {
if(value == null) {
return defaultValue;
}
Matcher m = SIZE_PATTERN.matcher(value);
if(m.find()) {
long num = Long.parseLong(m.group(1));
int man = SIZE_POWER_MAP.indexOf(m.group(3).toUpperCase());
if(man < 0) {
man = 0;
}
int thresh = "I".equals(m.group(4).toUpperCase()) ? 1024 : 1000;
BigDecimal mul = BigDecimal.valueOf(thresh).pow(man);
try {
return num * mul.longValueExact();
} catch (ArithmeticException e) {
LOGGER.warn("Value of too big ({})", value);
return overflowValue;
}
} else {
return defaultValue;
}
}
示例2: tryParse
import java.math.BigDecimal; //导入方法依赖的package包/类
public static boolean tryParse(String s, Fixed8 result) {
try {
BigDecimal val = new BigDecimal(s);
result.value = val.longValueExact();
return true;
} catch(NumberFormatException | ArithmeticException ex) {
return false;
}
}
示例3: convertFromFraction
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Converts a fraction from 0 to 1 for this field to a value.
* <p>
* The fractional value must be between 0 (inclusive) and 1 (exclusive).
* It can only be returned if the {@link java.time.temporal.TemporalField#range() value range} is fixed.
* The value is obtained by calculation from the field range and a rounding
* mode of {@link RoundingMode#FLOOR FLOOR}.
* The calculation is inaccurate if the values do not run continuously from smallest to largest.
* <p>
* For example, the fractional second-of-minute of 0.25 would be converted to 15,
* assuming the standard definition of 60 seconds in a minute.
*
* @param fraction the fraction to convert, not null
* @return the value of the field, valid for this rule
* @throws DateTimeException if the value cannot be converted
*/
private long convertFromFraction(BigDecimal fraction) {
ValueRange range = field.range();
BigDecimal minBD = BigDecimal.valueOf(range.getMinimum());
BigDecimal rangeBD = BigDecimal.valueOf(range.getMaximum()).subtract(minBD).add(BigDecimal.ONE);
BigDecimal valueBD = fraction.multiply(rangeBD).setScale(0, RoundingMode.FLOOR).add(minBD);
return valueBD.longValueExact();
}