001: /* BigDecimals.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Apr 17 10:25:07 2003, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2002 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: import java.math.BigInteger;
023: import java.util.Locale;
024:
025: import org.zkoss.lang.Objects;
026: import org.zkoss.math.BigIntegers;
027:
028: /**
029: * Utilities and constants of big decimals.
030: *
031: * @author tomyeh
032: */
033: public class BigDecimals {
034: /** Represents 0 in big decimal.
035: * @see #ONE
036: * @see #MINUS_ONE
037: */
038: public static final BigDecimal ZERO = Objects.ZERO_BIG_DECIMAL;
039: /** Represents 1 in big decimal.
040: * @see #ZERO
041: * @see #MINUS_ONE
042: */
043: public static final BigDecimal ONE = new BigDecimal(BigInteger.ONE);
044: /** Represents -1 in big decimal.
045: * @see #ZERO
046: * @see #ONE
047: */
048: public static final BigDecimal MINUS_ONE = new BigDecimal(
049: BigInteger.ONE.negate());
050:
051: /** Represents our number precision.
052: */
053: public static final int NUMBER_PRECISION = 38;
054:
055: /** Represents our number scale.
056: */
057: public static final int NUMBER_SCALE = 6;
058:
059: /** Represents our fine number precision.
060: */
061: public static final int FINE_NUMBER_PRECISION = 20;
062: /** Represents our fine number scale.
063: */
064: public static final int FINE_NUMBER_SCALE = 8;
065:
066: /** Converts a double to a big decimal with a scale.
067: *
068: * <p>It is strongly deprecated to use new Dicimal(double) since
069: * the scale is unpredictable and usually surprising.
070: * Example, BigDecimal(.1) will becomes
071: * .1000000000000000055511151231257827021181583404541015625.
072: * On the other hand, BigDecimal("0.1") will be 0.1 correctly.
073: *
074: * @param scale the BigDecimal's scale
075: * @param roundingMode the rounding mode
076: */
077: public static final BigDecimal toBigDecimal(double v, int scale,
078: int roundingMode) {
079: return new BigDecimal(v).setScale(scale, roundingMode);
080: }
081:
082: /** Converts a double to a big decimal with a scale.
083: * It uses {@link BigDecimal#ROUND_HALF_UP}.
084: */
085: public static final BigDecimal toBigDecimal(double v, int scale) {
086: return toBigDecimal(v, scale, BigDecimal.ROUND_HALF_UP);
087: }
088:
089: /** Converts an integer to a big decimal with a scale.
090: */
091: public static final BigDecimal toBigDecimal(int v, int scale) {
092: return new BigDecimal(BigIntegers.toBigInteger(v), scale);
093: }
094:
095: /** Converts an integer to a big decimal with a scale.
096: */
097: public static final BigDecimal toBigDecimal(long v, int scale) {
098: return new BigDecimal(BigIntegers.toBigInteger(v), scale);
099: }
100:
101: /** Converts an integer to a big decimal with a scale without.
102: * losing precision -- zero scale in this case.
103: */
104: public static final BigDecimal toBigDecimal(int v) {
105: return v == 0 ? ZERO : new BigDecimal(BigIntegers
106: .toBigInteger(v));
107: }
108:
109: /** Converts a long to a big decimal with a scale without.
110: * losing precision -- zero scale in this case.
111: */
112: public static final BigDecimal toBigDecimal(long v) {
113: return v == 0 ? ZERO : new BigDecimal(BigIntegers
114: .toBigInteger(v));
115: }
116:
117: /** Converts a short to a big decimal with a scale without.
118: * losing precision -- zero scale in this case.
119: */
120: public static final BigDecimal toBigDecimal(short v) {
121: return v == 0 ? ZERO : new BigDecimal(BigIntegers
122: .toBigInteger(v));
123: }
124:
125: /** Converts a byte to a big decimal with a scale without.
126: * losing precision -- zero scale in this case.
127: */
128: public static final BigDecimal toBigDecimal(byte v) {
129: return v == 0 ? ZERO : new BigDecimal(BigIntegers
130: .toBigInteger(v));
131: }
132:
133: /** Converts an integer to a big decimal with a scale without.
134: * losing precision -- zero scale in this case.
135: */
136: public static final BigDecimal toBigDecimal(Integer v) {
137: return toBigDecimal(v.intValue());
138: }
139:
140: /** Converts a long to a big decimal with a scale without.
141: * losing precision -- zero scale in this case.
142: */
143: public static final BigDecimal toBigDecimal(Long v) {
144: return toBigDecimal(v.longValue());
145: }
146:
147: /** Converts a short to a big decimal with a scale without.
148: * losing precision -- zero scale in this case.
149: */
150: public static final BigDecimal toBigDecimal(Short v) {
151: return toBigDecimal(v.shortValue());
152: }
153:
154: /** Converts a byte to a big decimal with a scale without.
155: * losing precision -- zero scale in this case.
156: */
157: public static final BigDecimal toBigDecimal(Byte v) {
158: return toBigDecimal(v.byteValue());
159: }
160: }
|