简体   繁体   中英

figure (number) to word java code

I want to convert figure to word up-to 1 lack crore mean up to 1000000000000 if I pass a number like this function should return thin=s number in word

I already know the codes up to 900000000 but i want that limit Here is that code I wrote ... Somebody please make this code that capable of converting 1000000000000 to word

public class NumberToWord  

{
    private static final String[] specialNames = {
        "",
        " thousand",
        " million",
        " billion",
        " trillion",
        " quadrillion",
        " quintillion"
    };

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
    };

    private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
    };

    private String convertLessThanOneThousand(int number) {
        String current;

        if (number % 100 < 20){
            current = numNames[number % 100];
            number /= 100;
        }
        else {
            current = numNames[number % 10];
            number /= 10;

            current = tensNames[number % 10] + current;
            number /= 10;
        }
        if (number == 0) return current;
        return numNames[number] + " hundred" + current;
    }

    public String convert(int number) {

        if (number == 0) { return "zero"; }

        String prefix = "";

        if (number < 0) {
            number = -number;
            prefix = "negative";
        }

        String current = "";
        int place = 0;

        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s + specialNames[place] + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + current).trim();
    }

    public static void main(String[] args) {
        NumberToWord obj = new NumberToWord();
        System.out.println("*** " + obj.convert(123456789));
        System.out.println("*** " + obj.convert(-55));
    }
}

You can use long as the data type.

private String convertLessThanOneThousand(long number) {
    String current;

    if (number % 100 < 20){
        current = numNames[(int) (number % 100)];
        number /= 100;
    }
    else {
        current = numNames[(int) (number % 10)];
        number /= 10;

        current = tensNames[(int) (number % 10)] + current;
        number /= 10;
    }
    if (number == 0) return current;
    return numNames[(int) number] + " hundred" + current;
}

public String convert(long number) {

    if (number == 0) { return "zero"; }

    String prefix = "";

    if (number < 0) {
        number = -number;
        prefix = "negative";
    }

    String current = "";
    int place = 0;

    do {
        long n = number % 1000;
        if (n != 0){
            String s = convertLessThanOneThousand(n);
            current = s + specialNames[place] + current;
        }
        place++;
        number /= 1000;
    } while (number > 0);

    return (prefix + current).trim();
}

public static void main(String[] args) {
    NumberToWord obj = new NumberToWord();
    System.out.println("*** " + obj.convert(123456789));
    System.out.println("*** " + obj.convert(-55));
    System.out.println("*** " + obj.convert(1000000000000L));
}

Output:

*** one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
*** negative fifty five
*** one trillion

Note the L towards the end of 1000000000000 in case your editor throws integer too long error.

Instead of doing it all by yourself you may consider using ICU4J. ICU4J is a library which provides Unicode and Globalization support.

ICU provides number spellout rules for several locales. Below is an example for english:

 import com.ibm.icu.text.NumberFormat;
 import com.ibm.icu.text.RuleBasedNumberFormat;
 import com.ibm.icu.util.ULocale;

 public class NewClass4 {       
    public static void main(String[] args){
    ULocale locale = new ULocale("En");
    String input = "1000000000000";
    Long d = Long.parseLong(input);
    NumberFormat formatter = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.SPELLOUT);
    String result = formatter.format(d);
    System.out.println(result);
 }
}

output : one trillion

You can use it also for double values. For

Double d = Double.parseDouble("123.45");

Output : one hundred twenty-three point four five

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM