📄 floatmul.java
字号:
* * @param val string representation of a <tt>BigDecimal</tt>. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @throws NumberFormatException if <tt>val</tt> is not a valid * representation of a BigDecimal. * @since 1.5 */ public BigDecimal(String val, MathContext mc) { this(val.toCharArray(), 0, val.length()); if (mc.precision > 0) roundThis(mc); } /** * Translates a <tt>double</tt> into a <tt>BigDecimal</tt> which * is the exact decimal representation of the <tt>double</tt>'s * binary floating-point value. The scale of the returned * <tt>BigDecimal</tt> is the smallest value such that * <tt>(10<sup>scale</sup> × val)</tt> is an integer. * <p> * <b>Notes:</b> * <ol> * <li> * The results of this constructor can be somewhat unpredictable. * One might assume that writing <tt>new BigDecimal(0.1)</tt> in * Java creates a <tt>BigDecimal</tt> which is exactly equal to * 0.1 (an unscaled value of 1, with a scale of 1), but it is * actually equal to * 0.1000000000000000055511151231257827021181583404541015625. * This is because 0.1 cannot be represented exactly as a * <tt>double</tt> (or, for that matter, as a binary fraction of * any finite length). Thus, the value that is being passed * <i>in</i> to the constructor is not exactly equal to 0.1, * appearances notwithstanding. * * <li> * The <tt>String</tt> constructor, on the other hand, is * perfectly predictable: writing <tt>new BigDecimal("0.1")</tt> * creates a <tt>BigDecimal</tt> which is <i>exactly</i> equal to * 0.1, as one would expect. Therefore, it is generally * recommended that the {@linkplain #BigDecimal(String) * <tt>String</tt> constructor} be used in preference to this one. * * <li> * When a <tt>double</tt> must be used as a source for a * <tt>BigDecimal</tt>, note that this constructor provides an * exact conversion; it does not give the same result as * converting the <tt>double</tt> to a <tt>String</tt> using the * {@link Double#toString(double)} method and then using the * {@link #BigDecimal(String)} constructor. To get that result, * use the <tt>static</tt> {@link #valueOf(double)} method. * </ol> * * @param val <tt>double</tt> value to be converted to * <tt>BigDecimal</tt>. * @throws NumberFormatException if <tt>val</tt> is infinite or NaN. */ public BigDecimal(double val) { if (Double.isInfinite(val) || Double.isNaN(val)) throw new NumberFormatException("Infinite or NaN"); // Translate the double into sign, exponent and significand, according // to the formulae in JLS, Section 20.10.22. long valBits = Double.doubleToLongBits(val); int sign = ((valBits >> 63)==0 ? 1 : -1); int exponent = (int) ((valBits >> 52) & 0x7ffL); long significand = (exponent==0 ? (valBits & ((1L<<52) - 1)) << 1 : (valBits & ((1L<<52) - 1)) | (1L<<52)); exponent -= 1075; // At this point, val == sign * significand * 2**exponent. /* * Special case zero to supress nonterminating normalization * and bogus scale calculation. */ if (significand == 0) { intVal = BigInteger.ZERO; intCompact = 0; precision = 1; return; } // Normalize while((significand & 1) == 0) { // i.e., significand is even significand >>= 1; exponent++; } // Calculate intVal and scale intVal = BigInteger.valueOf(sign*significand); if (exponent < 0) { intVal = intVal.multiply(BigInteger.valueOf(5).pow(-exponent)); scale = -exponent; } else if (exponent > 0) { intVal = intVal.multiply(BigInteger.valueOf(2).pow(exponent)); } if (intVal.bitLength() <= MAX_BIGINT_BITS) { intCompact = intVal.longValue(); } } /** * Translates a <tt>double</tt> into a <tt>BigDecimal</tt>, with * rounding according to the context settings. The scale of the * <tt>BigDecimal</tt> is the smallest value such that * <tt>(10<sup>scale</sup> × val)</tt> is an integer. * * <p>The results of this constructor can be somewhat unpredictable * and its use is generally not recommended; see the notes under * the {@link #BigDecimal(double)} constructor. * * @param val <tt>double</tt> value to be converted to * <tt>BigDecimal</tt>. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * RoundingMode is UNNECESSARY. * @throws NumberFormatException if <tt>val</tt> is infinite or NaN. * @since 1.5 */ public BigDecimal(double val, MathContext mc) { this(val); if (mc.precision > 0) roundThis(mc); } /** * Translates a <tt>BigInteger</tt> into a <tt>BigDecimal</tt>. * The scale of the <tt>BigDecimal</tt> is zero. * * @param val <tt>BigInteger</tt> value to be converted to * <tt>BigDecimal</tt>. */ public BigDecimal(BigInteger val) { intVal = val; if (val.bitLength() <= MAX_BIGINT_BITS) { intCompact = val.longValue(); } } /** * Translates a <tt>BigInteger</tt> into a <tt>BigDecimal</tt> * rounding according to the context settings. The scale of the * <tt>BigDecimal</tt> is zero. * * @param val <tt>BigInteger</tt> value to be converted to * <tt>BigDecimal</tt>. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @since 1.5 */ public BigDecimal(BigInteger val, MathContext mc) { intVal = val; if (mc.precision > 0) roundThis(mc); } /** * Translates a <tt>BigInteger</tt> unscaled value and an * <tt>int</tt> scale into a <tt>BigDecimal</tt>. The value of * the <tt>BigDecimal</tt> is * <tt>(unscaledVal × 10<sup>-scale</sup>)</tt>. * * @param unscaledVal unscaled value of the <tt>BigDecimal</tt>. * @param scale scale of the <tt>BigDecimal</tt>. */ public BigDecimal(BigInteger unscaledVal, int scale) { // Negative scales are now allowed intVal = unscaledVal; this.scale = scale; if (unscaledVal.bitLength() <= MAX_BIGINT_BITS) { intCompact = unscaledVal.longValue(); } } /** * Translates a <tt>BigInteger</tt> unscaled value and an * <tt>int</tt> scale into a <tt>BigDecimal</tt>, with rounding * according to the context settings. The value of the * <tt>BigDecimal</tt> is <tt>(unscaledVal × * 10<sup>-scale</sup>)</tt>, rounded according to the * <tt>precision</tt> and rounding mode settings. * * @param unscaledVal unscaled value of the <tt>BigDecimal</tt>. * @param scale scale of the <tt>BigDecimal</tt>. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @since 1.5 */ public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) { intVal = unscaledVal; this.scale = scale; if (mc.precision > 0) roundThis(mc); } /** * Translates an <tt>int</tt> into a <tt>BigDecimal</tt>. The * scale of the <tt>BigDecimal</tt> is zero. * * @param val <tt>int</tt> value to be converted to * <tt>BigDecimal</tt>. * @since 1.5 */ public BigDecimal(int val) { intCompact = val; } /** * Translates an <tt>int</tt> into a <tt>BigDecimal</tt>, with * rounding according to the context settings. The scale of the * <tt>BigDecimal</tt>, before any rounding, is zero. * * @param val <tt>int</tt> value to be converted to <tt>BigDecimal</tt>. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @since 1.5 */ public BigDecimal(int val, MathContext mc) { intCompact = val; if (mc.precision > 0) roundThis(mc); } /** * Translates a <tt>long</tt> into a <tt>BigDecimal</tt>. The * scale of the <tt>BigDecimal</tt> is zero. * * @param val <tt>long</tt> value to be converted to <tt>BigDecimal</tt>. * @since 1.5 */ public BigDecimal(long val) { if (compactLong(val)) intCompact = val; else intVal = BigInteger.valueOf(val); } /** * Translates a <tt>long</tt> into a <tt>BigDecimal</tt>, with * rounding according to the context settings. The scale of the * <tt>BigDecimal</tt>, before any rounding, is zero. * * @param val <tt>long</tt> value to be converted to <tt>BigDecimal</tt>. * @param mc the context to use. * @throws ArithmeticException if the result is inexact but the * rounding mode is <tt>UNNECESSARY</tt>. * @since 1.5 */ public BigDecimal(long val, MathContext mc) { if (compactLong(val)) intCompact = val; else intVal = BigInteger.valueOf(val); if (mc.precision > 0) roundThis(mc); } /** * Trusted internal constructor */ private BigDecimal(long val, int scale) { this.intCompact = val; this.scale = scale; } /** * Trusted internal constructor */ private BigDecimal(BigInteger intVal, long val, int scale) { this.intVal = intVal; this.intCompact = val; this.scale = scale; } // Static Factory Methods /** * Translates a <tt>long</tt> unscaled value and an * <tt>int</tt> scale into a <tt>BigDecimal</tt>. This * "static factory method" is provided in preference to * a (<tt>long</tt>, <tt>int</tt>) constructor because it * allows for reuse of frequently used <tt>BigDecimal</tt> values.. * * @param unscaledVal unscaled value of the <tt>BigDecimal</tt>. * @param scale scale of the <tt>BigDecimal</tt>. * @return a <tt>BigDecimal</tt> whose value is * <tt>(unscaledVal × 10<sup>-scale</sup>)</tt>. */ public static BigDecimal valueOf(long unscaledVal, int scale) { if (scale == 0 && unscaledVal >= 0 && unscaledVal <= 10) { return zeroThroughTen[(int)unscaledVal]; } if (compactLong(unscaledVal)) return new BigDecimal(unscaledVal, scale); return new BigDecimal(BigInteger.valueOf(unscaledVal), scale); } /** * Translates a <tt>long</tt> value into a <tt>BigDecimal</tt> * with a scale of zero. This "static factory method" * is provided in preference to a (<tt>long</tt>) constructor * because it allows for reuse of frequently used * <tt>BigDecimal</tt> values. * * @param val value of the <tt>BigDecimal</tt>. * @return a <tt>BigDecimal</tt> whose value is <tt>val</tt>. */ public static BigDecimal valueOf(long val) { return valueOf(val, 0); } /** * Translates a <tt>double</tt> into a <tt>BigDecimal</tt>, using * the <tt>double</tt>'s canonical string representation provided * by the {@link Double#toString(double)} method. * * <p><b>Note:</b> This is generally the preferred way to convert * a <tt>double</tt> (or <tt>float</tt>) into a * <tt>BigDecimal</tt>, as the value returned is equal to that * resulting from constructing a <tt>BigDecimal</tt> from the * result of using {@link Double#toString(double)}. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -