001: /* RoundingModes.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Fri May 4 12:24:48 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.math;
020:
021: import java.math.BigDecimal;
022:
023: /**
024: * Utilities to handle the rounding mode.
025: * @author tomyeh
026: */
027: public class RoundingModes {
028: private RoundingModes() {
029: }
030:
031: /** Returns the name of the rounding mode.
032: * Possible values include
033: <dl>
034: <dt>CEILING</dt>
035: <dd>Rounding mode to round towards positive infinity.</dd>
036: <dt>DOWN</dt>
037: <dd>Rounding mode to round towards zero.</dd>
038: <dt>FLOOR</dt>
039: <dd>Rounding mode to round towards negative infinity.</dd>
040: <dt>HALF_DOWN</dt>
041: <dd>Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.</dd>
042: <dt>HALF_EVEN</dt>
043: <dd>Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.</dd>
044: <dt>HALF_UP</dt>
045: <dd>Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.</dd>
046: <dt>UNNECESSARY</dt>
047: <dd>Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.</dd>
048: <dt>UP</dt>
049: <dd>Rounding mode to round away from zero.</dd>
050: </dl>
051: */
052: public static final String toString(int roundingMode) {
053: switch (roundingMode) {
054: case BigDecimal.ROUND_CEILING:
055: return "CEILING";
056: case BigDecimal.ROUND_DOWN:
057: return "DOWN";
058: case BigDecimal.ROUND_FLOOR:
059: return "FLOOR";
060: case BigDecimal.ROUND_HALF_DOWN:
061: return "HALF_DOWN";
062: case BigDecimal.ROUND_HALF_EVEN:
063: return "HALF_EVEN";
064: case BigDecimal.ROUND_HALF_UP:
065: return "HALF_UP";
066: case BigDecimal.ROUND_UNNECESSARY:
067: return "UNNECESSARY";
068: case BigDecimal.ROUND_UP:
069: return "UP";
070: default:
071: throw new IllegalArgumentException(
072: "Unknown rounding mode: " + roundingMode);
073: }
074: }
075:
076: /** Returns the rounding mode of the specified name.
077: *
078: * @param name the rounding mode's name. Allowed values include:
079: <dl>
080: <dt>CEILING</dt>
081: <dd>Rounding mode to round towards positive infinity.</dd>
082: <dt>DOWN</dt>
083: <dd>Rounding mode to round towards zero.</dd>
084: <dt>FLOOR</dt>
085: <dd>Rounding mode to round towards negative infinity.</dd>
086: <dt>HALF_DOWN</dt>
087: <dd>Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.</dd>
088: <dt>HALF_EVEN</dt>
089: <dd>Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.</dd>
090: <dt>HALF_UP</dt>
091: <dd>Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.</dd>
092: <dt>UNNECESSARY</dt>
093: <dd>Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.</dd>
094: <dt>UP</dt>
095: <dd>Rounding mode to round away from zero.</dd>
096: </dl>
097: */
098: public static final int valueOf(String name) {
099: name = name.toUpperCase();
100: if ("CEILING".equals(name))
101: return BigDecimal.ROUND_CEILING;
102: if ("DOWN".equals(name))
103: return BigDecimal.ROUND_DOWN;
104: if ("FLOOR".equals(name))
105: return BigDecimal.ROUND_FLOOR;
106: if ("HALF_DOWN".equals(name))
107: return BigDecimal.ROUND_HALF_DOWN;
108: if ("HALF_EVEN".equals(name))
109: return BigDecimal.ROUND_HALF_EVEN;
110: if ("HALF_UP".equals(name))
111: return BigDecimal.ROUND_HALF_UP;
112: if ("UNNECESSARY".equals(name))
113: return BigDecimal.ROUND_UNNECESSARY;
114: if ("UP".equals(name))
115: return BigDecimal.ROUND_UP;
116: throw new IllegalArgumentException("Unknown rounding mode: "
117: + name);
118: }
119: }
|