Java BigDecimal numberToWordsWithDecimal(BigDecimal value)

Here you can find the source of numberToWordsWithDecimal(BigDecimal value)

Description

number To Words With Decimal

License

Creative Commons License

Declaration

public static String numberToWordsWithDecimal(BigDecimal value) 

Method Source Code

//package com.java2s;
/*/*from  ww  w .  jav  a 2  s. com*/
 * This software is in the public domain under CC0 1.0 Universal plus a
 * Grant of Patent License.
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software (see the LICENSE.md file). If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

import java.math.BigDecimal;

public class Main {
    private static final String[] SCALES = new String[] { "", "thousand", "million", "billion", "trillion",
            "quadrillion", "quintillion", "sextillion" };
    private static final String[] SUBTWENTY = new String[] { "", "one", "two", "three", "four", "five", "six",
            "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
            "seventeen", "eighteen", "nineteen" };
    private static final String[] DECADES = new String[] { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
            "seventy", "eighty", "ninety" };
    private static final String NEG_NAME = "negative";

    public static String numberToWordsWithDecimal(BigDecimal value) {
        final String integerText = numberToWords(value.longValue(), false);
        String decimalText = value.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
        decimalText = decimalText.substring(decimalText.indexOf(".") + 1);
        return integerText + " and " + decimalText + "/100";
    }

    /** Convert any long input value to a text representation
     * @param value  The value to convert
     * @param useAnd true if you want to use the word 'and' in the text (eleven thousand and thirteen)
     */
    public static String numberToWords(long value, boolean useAnd) {
        if (value == 0L)
            return SUBTWENTY[0];

        // break the value down in to sets of three digits (thousands)
        Integer[] thous = new Integer[SCALES.length];
        boolean neg = value < 0;
        // do not make negative numbers positive, to handle Long.MIN_VALUE
        int scale = 0;
        while (value != 0) {
            // use abs to convert thousand-groups to positive, if needed.
            thous[scale] = Math.abs((int) (value % 1000));
            value = value / 1000;
            scale++;
        }

        StringBuilder sb = new StringBuilder(scale * 40);
        if (neg)
            sb.append(NEG_NAME).append(" ");
        boolean first = true;
        while ((scale = --scale) > 0) {
            if (!first)
                sb.append(", ");
            first = false;
            if (thous[scale] > 0)
                sb.append(tripleAsWords(thous[scale], useAnd)).append(" ").append(SCALES[scale]);
        }

        if (!first && thous[0] != 0) {
            if (useAnd)
                sb.append(" and ");
            else
                sb.append(" ");
        }

        sb.append(tripleAsWords(thous[0], useAnd));

        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }

    /** Convert any value from 0 to 999 inclusive, to a string. */
    private static String tripleAsWords(int value, boolean useAnd) {
        if (value < 0 || value >= 1000)
            throw new IllegalArgumentException("Illegal triple-value " + value);
        if (value < SUBTWENTY.length)
            return SUBTWENTY[value];

        int subhun = value % 100;
        int hun = value / 100;
        StringBuilder sb = new StringBuilder(50);
        if (hun > 0)
            sb.append(SUBTWENTY[hun]).append(" hundred");
        if (subhun > 0) {
            if (hun > 0)
                sb.append(useAnd ? " and " : " ");
            if (subhun < SUBTWENTY.length) {
                sb.append(" ").append(SUBTWENTY[subhun]);
            } else {
                int tens = subhun / 10;
                int units = subhun % 10;
                if (tens > 0)
                    sb.append(DECADES[tens]);
                if (units > 0)
                    sb.append(" ").append(SUBTWENTY[units]);
            }
        }
        return sb.toString();
    }
}

Related

  1. milliToDollar(BigDecimal val)
  2. mod(long res, BigDecimal value)
  3. movePoint(final BigDecimal v1, final int shift)
  4. moveRight(BigDecimal value, int offset)
  5. nullSafeCompare(BigDecimal pPremierNombre, BigDecimal pSecondNombre)
  6. nvl(final BigDecimal bigDecimal)
  7. nvlZero(BigDecimal num)
  8. objectToBigDecimal(Object object)
  9. objToBigDecimal(Object obj)