當前位置: 首頁>>代碼示例>>Java>>正文


Java BigDecimal.longValueExact方法代碼示例

本文整理匯總了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;
    }
}
 
開發者ID:jhkst,項目名稱:dlface,代碼行數:40,代碼來源:Util.java

示例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;
    }
}
 
開發者ID:DNAProject,項目名稱:DNASDKJava,代碼行數:10,代碼來源:Fixed8.java

示例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();
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:24,代碼來源:DateTimeFormatterBuilder.java


注:本文中的java.math.BigDecimal.longValueExact方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。