Java String Multiply multiply(String str11, String str22)

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

Description

multiply

License

Apache License

Declaration

public static String multiply(String str11, String str22) 

Method Source Code

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

public class Main {
    public static String multiply(String str11, String str22) {

        String result = "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);
        }/*from  w  ww .  j a  va 2s . c  om*/
        char[] ch1 = new char[str1.length()];
        reverse(str1, ch1);

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

        String[] res = new String[ch1.length * ch1.length];
        int r = 0;
        for (int i = 0; i < ch1.length; i++) {
            for (int j = 0; j < ch2.length; j++) {
                res[r] = String.valueOf(morePlus(ch1[i] - '0', ch2[j] - '0'));

                for (int k = 0; k <= i + j; k++) {
                    if (k > 0) {
                        res[r] = res[r] + "0";
                    }
                }
                if (r < res.length) {
                    r++;
                }
            }
        }

        for (String s : res) {
            result = sum(result, s);
        }
        int index0 = -1;
        for (int i = 0; i < result.length(); i++) {
            if (result.charAt(0) == '0')
                if (result.charAt(i) == '0' && result.charAt(i + 1) != '0') {
                    index0 = i;
                }
        }
        if (index0 != -1) {
            result = result.substring(index0 + 1);
        }
        int index = 0;
        if (str11.contains(".") || str22.contains(".")) {
            int index1 = str11.indexOf(".");
            int len1 = str11.length() - 1 - index1;
            int index2 = str22.indexOf(".");
            int len2 = str22.length() - 1 - index2;
            index = result.length() - (len1 + len2);

            StringBuilder ss = new StringBuilder(result);
            ss.insert(index, ".");
            result = ss.toString();
        }

        return result;
    }

    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);
        }
    }

    public static int morePlus(int a, int b) {
        int temp = a;
        int result = 0;
        for (int i = 0; i < b; i++) {
            result += temp;
        }
        return result;

    }

    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);
            }
        } 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;
    }
}

Related

  1. multiply(final CharSequence str, final int factor)
  2. multiply(final String s, final Long factor)
  3. multiply(final String string, final int times)
  4. multiply(String original, int times)
  5. multiply(String self, int reps)
  6. multiplyString(String s, int count)
  7. multiplyString(String sourceStr, int count)
  8. multiplyStringWithInt(String strMultiplicand, int multiplier)