How to summarize Huge Numbers in Java

How to summarize Huge Numbers in Java

Hello everyone, in this article we are going to talk about summarising huge numbers which can not be declared with normal decimal numbers and can be declared as string variable types in Java.

Let's get started...

Firstly, Below list will show the min max values of variable types in Java:
  • byte is a 1 byte variable and stores numbers from -128 to 127
  • short is a 2 bytes variable and stores numbers from -32,768 to 32,767
  • int is a 4 bytes variable and stores numbers from -2,147,483,648 to 2,147,483,647
  • long is a 8 bytes variable and stores numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • float is a 4 bytes variable and stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
  • double is a 8 bytes variable and stores fractional numbers. Sufficient for storing 15 decimal digits
  • boolean is a 1 bit variable and stores true or false values
  • char is a 2 bytes variable and stores a single character/letter or ASCII values

So what if we need to make summarising numbers which is greater than double and long maximum values

In here I will use below algorithm, In this algorithm we will consider the numbers as strings and we will make the calculation with all digits like teachers taught us at primary school.

Now let's write some codes.

In here we have three functions. These are a main method to call the function. A summarizeHugeNumbers method to make the calculation with String parameters which hold the huge numbers. And a reverseString to reverse the string in calculation.

Below code block you can see the class and its methods.

public class SummarizeHugeNumbers {
    
    public static void main(String[] args) {

    }
    
    public static void summarizeHugeNumbers(String _no1, String _no2){

    }
        
    public static String reverseString(String word){

    }
}

Firstly we are going to write summarize method.

Below code block will have two String parameters which pass the numbers which be summarized. And then reverse them firstly. This is required to start to summarize from Least Significant Digit. We will re-reverse the number after calculation.

And then we are going to make them same length, so we will add zero's until they are equal. And then we are going to make the calculation digit by digit. If summarisation of digits greater than 10 it will pass the 1 to next value.

Below code block you can see the algorithm

    public static void summarizeHugeNumbers(String _no1, String _no2){
    
        String output = "";
        
        //reversed number variables
        String rno1, rno2;
        //if summarized digits more than 10 it will move one next
        boolean hasDecimal = false;
                
        //firstly we need to reverse
        rno1 = reverseString(_no1);
        rno2 = reverseString(_no2);
        
        //find the for loop size according to number character count
        int loopSize = rno1.length() >= rno2.length() ? rno1.length() : rno2.length();
        
        //Fill with 0 rest of decimal areas and make them equal lenght
        while(rno1.length() != rno2.length() ){
            if(rno1.length() < rno2.length()) rno1 += "0";
            else if(rno1.length() > rno2.length()) rno2 += "0";
        }

        for(int i=0; i<loopSize; i++){
            //summarize the decimals first
            int res = Integer.parseInt(rno1.charAt(i) + "") + Integer.parseInt( rno2.charAt(i)+ "" ); 
            res += hasDecimal ? 1 : 0; //add 1 if hasDecimal
            hasDecimal = (res > 9);//set the decimal if bigger than 10
            //calculate and add the decimal to output variable
            int decimal = res % 10;
            output += decimal + "";
        }
        
        output = reverseString(output);
        
        System.out.println(output);
    }

Now below we will see the reverseString method.

This method first define the output variable and then start to add the chars from end of string.


    public static String reverseString(String word){
        //First define the output variable
        String output = "";
        //then start to add char by char to output variable
        for(int i=word.length()-1; i>=0; i--){
            output += word.charAt(i);
        }
        return output;
    }
Lastly we weii call the method from main function.

public static void main(String[] args) {
        String no1 = "12452452452452452452452452425245245245254245245243252234356546765536456785673578568468452452452452"; 
        String no2 = "3454543245454545245452453245453453245454837827245245234524524524545242452452455242452452424524524532453245245245245234532453245345324";
        summarizeHugeNumbers(no1, no2);
    }
Below image you can see the output of the program. Example output of summarize Huge Numbers in Java

That is all in this article.

Burak Hamdi TUFAN.


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...