當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。