当前位置: 首页>>代码示例>>Java>>正文


Java RoundingMode.HALF_EVEN属性代码示例

本文整理汇总了Java中java.math.RoundingMode.HALF_EVEN属性的典型用法代码示例。如果您正苦于以下问题:Java RoundingMode.HALF_EVEN属性的具体用法?Java RoundingMode.HALF_EVEN怎么用?Java RoundingMode.HALF_EVEN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在java.math.RoundingMode的用法示例。


在下文中一共展示了RoundingMode.HALF_EVEN属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

public static void main(String [] argv) {

        // For each member of the family, make sure
        // rm == valueOf(rm.toString())

        for(RoundingMode rm: RoundingMode.values()) {
            if (rm != RoundingMode.valueOf(rm.toString())) {
                throw new RuntimeException("Bad roundtrip conversion of " +
                                           rm.toString());
            }
        }

        // Test that mapping of old integers to new values is correct
        if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) !=
                RoundingMode.CEILING) {
                throw new RuntimeException("Bad mapping for ROUND_CEILING");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) !=
                RoundingMode.DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) !=
                RoundingMode.FLOOR) {
                throw new RuntimeException("Bad mapping for ROUND_FLOOR");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) !=
                RoundingMode.HALF_DOWN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) !=
                RoundingMode.HALF_EVEN) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) !=
                RoundingMode.HALF_UP) {
                throw new RuntimeException("Bad mapping for ROUND_HALF_UP");
        }

        if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) !=
                RoundingMode.UNNECESSARY) {
                throw new RuntimeException("Bad mapping for ROUND_UNNECESARY");
        }
    }
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:48,代码来源:RoundingModeTests.java

示例2: checkAndSetFastPathStatus

/**
 * Check validity of using fast-path for this instance. If fast-path is valid
 * for this instance, sets fast-path state as true and initializes fast-path
 * utility fields as needed.
 *
 * This method is supposed to be called rarely, otherwise that will break the
 * fast-path performance. That means avoiding frequent changes of the
 * properties of the instance, since for most properties, each time a change
 * happens, a call to this method is needed at the next format call.
 *
 * FAST-PATH RULES:
 *  Similar to the default DecimalFormat instantiation case.
 *  More precisely:
 *  - HALF_EVEN rounding mode,
 *  - isGroupingUsed() is true,
 *  - groupingSize of 3,
 *  - multiplier is 1,
 *  - Decimal separator not mandatory,
 *  - No use of exponential notation,
 *  - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
 *  - For number of fractional digits, the exact values found in the default case:
 *     Currency : min = max = 2.
 *     Decimal  : min = 0. max = 3.
 *
 */
private boolean checkAndSetFastPathStatus() {

    boolean fastPathWasOn = isFastPath;

    if ((roundingMode == RoundingMode.HALF_EVEN) &&
        (isGroupingUsed()) &&
        (groupingSize == 3) &&
        (multiplier == 1) &&
        (!decimalSeparatorAlwaysShown) &&
        (!useExponentialNotation)) {

        // The fast-path algorithm is semi-hardcoded against
        //  minimumIntegerDigits and maximumIntegerDigits.
        isFastPath = ((minimumIntegerDigits == 1) &&
                      (maximumIntegerDigits >= 10));

        // The fast-path algorithm is hardcoded against
        //  minimumFractionDigits and maximumFractionDigits.
        if (isFastPath) {
            if (isCurrencyFormat) {
                if ((minimumFractionDigits != 2) ||
                    (maximumFractionDigits != 2))
                    isFastPath = false;
            } else if ((minimumFractionDigits != 0) ||
                       (maximumFractionDigits != 3))
                isFastPath = false;
        }
    } else
        isFastPath = false;

    resetFastPathData(fastPathWasOn);
    fastPathCheckNeeded = false;

    /*
     * Returns true after successfully checking the fast path condition and
     * setting the fast path data. The return value is used by the
     * fastFormat() method to decide whether to call the resetFastPathData
     * method to reinitialize fast path data or is it already initialized
     * in this method.
     */
    return true;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:67,代码来源:DecimalFormat.java


注:本文中的java.math.RoundingMode.HALF_EVEN属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。