Java Number Sum sum(String str11, String str22)

Here you can find the source of sum(String str11, String str22)

Description

sum

License

Apache License

Declaration

public static String sum(String str11, String str22) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String sum(String str11, String str22) {
        int index1 = str11.indexOf(".");
        int len1 = str11.length() - 1 - index1;
        int index2 = str22.indexOf(".");
        int len2 = str22.length() - 1 - index2;
        if (str11.contains(".") && str22.contains(".")) {
            if (len1 > len2) {
                str22 = compare2(str11, str22, len1, len2);
            } else if (len1 < len2) {
                str11 = compare2(str22, str11, len2, len1);
            }//ww  w.j  a  va2  s  .c o m
        } else if (str11.contains(".") && !str22.contains(".")) {
            str22 = str22 + ".";
            for (int i = 0; i < len1; i++) {
                str22 = str22 + "0";
            }
        } else if (!str11.contains(".") && str22.contains(".")) {
            str11 = str11 + ".";
            for (int i = 0; i < len2; i++) {
                str11 = str11 + "0";
            }
        }
        String str1 = str11.replace(".", "");
        String str2 = str22.replace(".", "");
        if (str1.length() > str2.length()) {
            str2 = compare1(str1, str2);
        } else if (str1.length() < str2.length()) {
            str1 = compare1(str2, str1);
        }

        char[] ch1 = new char[str1.length()];
        reverse(str1, ch1);

        char[] ch2 = new char[str2.length()];
        reverse(str2, ch2);

        String[] res = new String[str1.length() > str2.length() ? str1.length() + 1 : str2.length() + 1];
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < ch1.length; i++) {
            res[i] = String.valueOf((ch1[i] - '0') + (ch2[i] - '0'));
            if (Integer.parseInt(res[i]) >= 10 && i < ch1.length - 1) {
                res[i] = String.valueOf(Integer.parseInt(res[i]) - 10);
                ch1[i + 1] = (char) (ch1[i + 1] - '0' + '1' - '0' + 48);
            } else {
                StringBuilder bui = new StringBuilder(String.valueOf(res[i]));
                res[i] = bui.reverse().toString();
            }
            sb.append(res[i]);
        }
        String result = sb.reverse().toString();
        int index = 0;
        if (str11.contains(".") || str22.contains(".")) {
            if (len1 >= len2) {
                index = result.length() - len1;
            } else {
                index = result.length() - len2;
            }
            StringBuilder ss = new StringBuilder(result);
            ss.insert(index, ".");
            result = ss.toString();
        }
        return result;
    }

    private static String compare2(String str1, String str2, int len1, int len2) {
        int cha = len1 - len2;
        for (int i = 0; i < cha; i++) {
            str2 = str2 + "0";
        }
        return str2;
    }

    private static String compare1(String str1, String str2) {
        int num = str1.length() - str2.length();
        for (int i = 0; i < num; i++) {
            str2 = "0" + str2;
        }
        return str2;
    }

    private static void reverse(String str1, char[] ch1) {
        for (int i = str1.length() - 1; i >= 0; i--) {
            ch1[i] = str1.charAt(str1.length() - 1 - i);
        }
    }
}

Related

  1. sum(long space1, String space2)
  2. sum(long x, int c)
  3. sum(Number n1, Number n2)
  4. Sum(Object in, int min, int max)
  5. sum(String fieldName)
  6. sum0ToN(int n)
  7. sum_quad(long max)
  8. sum_quad_endrec(long max)
  9. sumaMesFull(String sF, int n)