Java Number to Word numberToWords(long value, boolean useAnd)

Here you can find the source of numberToWords(long value, boolean useAnd)

Description

Convert any long input value to a text representation

License

Creative Commons License

Parameter

Parameter Description
value The value to convert
useAnd true if you want to use the word 'and' in the text (eleven thousand and thirteen)

Declaration

public static String numberToWords(long value, boolean useAnd) 

Method Source Code

//package com.java2s;
/*/*w w  w.  j  av a 2s  .c o  m*/
 * 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/>.
 */

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";

    /** 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. numberToWord(long number)