abs() 공개 메소드

public abs ( ) : global::java.math.BigDecimal
리턴 global::java.math.BigDecimal
예제 #1
0
        /**
         * Returns the string representation of this rational number as integer and fraction parts in the form "integerPart fractionNominator/fractionDenominator".
         *
         * <p>The integer part is omitted if it is 0 (when this absolute rational number is smaller than 1).</p>
         * <p>The fraction part is omitted it it is 0 (when this rational number is an integer).</p>
         * <p>If this rational number is 0, then "0" is returned.</p>
         *
         * <p>Example: <code>BigRational.valueOf(3.5).toIntegerRationalString()</code> returns <code>"3 1/2"</code>.</p>
         *
         * @return the integer and fraction rational string representation
         * @see #valueOf(int, int, int)
         */
        public String toIntegerRationalString()
        {
            BigDecimal fractionNumerator = numerator.remainder(denominator);
            BigDecimal integerNumerator  = numerator.subtract(fractionNumerator);
            BigDecimal integerPart       = integerNumerator.divide(denominator);

            StringBuilder result = new StringBuilder();

            if (integerPart.signum() != 0)
            {
                result.Append(integerPart);
            }
            if (fractionNumerator.signum() != 0)
            {
                if (result.Length > 0)
                {
                    result.Append(' ');
                }
                result.Append(fractionNumerator.abs());
                result.Append('/');
                result.Append(denominator);
            }
            if (result.Length == 0)
            {
                result.Append('0');
            }

            return(result.ToString());
        }