Skip to content
Advertisement

How to enable my program to receive small to extensive, large numbers > 2.14 billion

I’m currently coding a program that converts a number to its word format, and thus far, I’ve written a code that only accepts no larger than the max value of an integer (2.14 billion). I was wondering how I would scale up the program’s capability to accepting larger numbers.

(Edited): I’ve converted all my int values to long datatypes. I was able to scale the limitation up from 2.14 to 999 billion, though shouldn’t be the max value of a long datatype is 9 quintillion?

The converter code:

public class Converter {

    public static WordList wordFormat = new WordList();

    public static final void converter(long value) {

        long valueSize = String.valueOf(value).length();

        String[] output = new String[(int) valueSize];

        long outputSize = output.length;
        long tracker = 1;
        long commasCTR = 0;
        long preValHolder = 0;

        // Returns "Zero" if value = 0
        if (value == 0) {
            System.out.println("Zero");
            return;
        };

        // Self-made Conversion Method
        for (long valHolder = value, placeVal = 0; placeVal <= outputSize - 1; placeVal++, tracker++) {

            long remainder = valHolder % 10;
            long nextDigit = valHolder / 10 % 10;
            valHolder = valHolder / 10;
            // Num to Word Conditional Statements
            switch ((int) tracker) {
                case 1: 
                    if (placeVal == 0) {
                        output[(int) placeVal] = WordList.wordFormat[0][(int) remainder]; // Ones
                    } else {
                        output[(int) placeVal] = WordList.wordFormat[0][(int) remainder].concat(" " + WordList.commas[(int) commasCTR]); // Commas
                        commasCTR++;
                    }
                    break;
                case 2: 
                    if (remainder == 1) { // Teens
                        output[(int) placeVal] = WordList.wordFormat[1][(int) preValHolder + 1];
                        if (placeVal < 3) output[(int) placeVal - 1] = " ";
                        else output[(int) placeVal - 1] = WordList.commas[(int) commasCTR - 1];
                    } else if (placeVal > 3 && preValHolder == 0 && remainder == 0 && nextDigit == 0) { // Comma Zero Value Handler
                        output[(int) placeVal - 1] = " ";
                        output[(int) placeVal] = WordList.wordFormat[2][(int) remainder];
                    } else {
                        output[(int) placeVal] = WordList.wordFormat[2][(int) remainder]; // Decades
                    }
                    break;
                case 3: 
                    output[(int) placeVal] = WordList.wordFormat[3][(int) remainder]; // Hundredths
                    break;
            }
            // Re-assigning Values
            if (tracker == 3) tracker = 0;
            preValHolder = remainder;
        }
        for (long i = outputSize - 1; i >= 0; i--) {
            if (output[(int) i] == " ") continue;
            System.out.print(output[(int) i].trim() + " ");
        }
        return;
    }
}
}```

Advertisement

Answer

I think you could use BigInteger https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement