public static String add(String num1, String num2){ String result = null; if (num1.contains(".")){ int des1 = getPoint(num1); String subNum1 = num1.substring(0,des1); String leftNum1 = num1.substring(des1+1); if (num2.contains(".")){ int des2 =getPoint(num2); String subNum2 = num2.substring(0,des2); String leftNum2 = num2.substring(des2+1); int max = leftNum1.length() > leftNum2.length()?leftNum1.length() : leftNum2.length(); if (subNum1.length() > subNum2.length()) subNum2 = addZeroAtLeft(subNum2,subNum1.length()-subNum2.length()); else subNum1 = addZeroAtLeft(subNum1, subNum2.length()-subNum1.length()); if (leftNum1.length() > leftNum2.length()) leftNum2 = addZeroAtRight(leftNum2,leftNum1.length()-leftNum2.length()); else leftNum1 = addZeroAtRight(leftNum1,leftNum2.length()-leftNum1.length()); String newNum1 = subNum1+leftNum1; String newNum2 = subNum2+leftNum2; result = getAddResult(newNum1,newNum2,newNum1.length()); StringBuffer sb = new StringBuffer(result); int pos = result.length(); for (int i=0;i<max;i++){ pos --; } sb.insert(pos,'.'); result = new String(sb); } else { if (subNum1.length() > num2.length()) num2 = addZeroAtLeft(num2,subNum1.length()-num2.length()); else subNum1 = addZeroAtLeft(subNum1,num2.length()-subNum1.length()); result = getAddResult(subNum1,num2,subNum1.length()) + "." + leftNum1; } } else { if (num2.contains(".")){ int des2 =getPoint(num2); String subNum2 = num2.substring(0,des2); String leftNum2 = num2.substring(des2+1); if (num1.length() > subNum2.length()) subNum2 = addZeroAtLeft(subNum2,num1.length()-subNum2.length()); else num1 = addZeroAtLeft(num1,subNum2.length()-num1.length()); result = getAddResult(num1,subNum2,num1.length()) + "." + leftNum2; } else{ if (num1.length() > num2.length()) num2 = addZeroAtLeft(num2,num1.length()-num2.length()); else num1 = addZeroAtLeft(num1,num2.length()-num1.length()); result = getAddResult(num1,num2,num1.length()); } } return result; } public static String addZeroAtLeft(String str, int len){ StringBuffer stringBuffer = new StringBuffer(str); for (int i=0;i<len;i++) stringBuffer.insert(0,'0'); return new String(stringBuffer); } public static String addZeroAtRight(String str, int len){ StringBuffer stringBuffer = new StringBuffer(str); for (int i=0;i<len;i++) stringBuffer.append('0'); return new String(stringBuffer); } public static String getAddResult(String num1, String num2, int len){ char[] result = new char[len+1]; for (int i=0;i<=len;i++) result[i] = '0'; for (int i=len-1;i>=0;i--){ int a = Integer.parseInt(num1.charAt(i) + ""); int b = Integer.parseInt(num2.charAt(i) + ""); int c = Character.digit(result[i+1],10); int sum = a + b + c; result[i+1] = (char) ('0' + sum % 10); result[i] = (char) ('0' + sum / 10); } StringBuffer sb = new StringBuffer(); for (int i = 0;i<=len;i++){ if (result[i]=='0'&&i==0) continue; sb.append(result[i]); } return new String(sb); } public static int getPoint(String str){ int len = str.length(); int i; for (i=len-1;i>0;i--){ if (str.charAt(i) == '.') break; } return i; }