能不能帮我分析下 下面这个题目啊
/**  (3)
   *  将money格式化成逗点加小数点形式
   *  如:formatMoney("123456")将返回"123,456.00" 
   *      formatMoney("1234567.10")将返回"1,234,567.10"
   *  当输入为不合法的数字时抛出异常
   */
public String formatMoney(String money) throw Exception;

解决方案 »

  1.   

    public String formatMoney(String money) throw Exception
    {
    try{
      Interger.pase(money); 
    }catch(Exception e)
    {
       throw IlleageArguException();
    }DicimalFormater formater = new Dicimalformater("###,###,###.00");return formater.format(money);
    }
      

  2.   

    package test;import java.text.DecimalFormat;public class Testtt { public String formatMoney(String money) throws Exception {
    Double num;
    try {
    num =Double.parseDouble(money);
    } catch (Exception e) {
    throw new IllegalArgumentException("please input a number");
    } DecimalFormat formater = new DecimalFormat("###,###.00"); return formater.format(num);
    } public static void main(String[] args) {
    Testtt tt = new Testtt();
    try {
    System.out.println(tt.formatMoney("123456"));
    System.out.println(tt.formatMoney("1234567.10"));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    使用DecimalFormat或者,你有时间,也可以试试NumberFormat
    同意2楼的做法。
      

  4.   


    public static void formatMoney(String money) {
            if (!money.matches("(?!(\\.|[0]{2,}))[\\d\\.]*(?<!\\.)")){
                System.out.println("Invalid Number");
                return;
            }
            int integralNumber = money.indexOf('.');
            int newCharLength = (integralNumber - 1) / 3 + money.length();        char[] c = new char[newCharLength];        for (int i = integralNumber % 3; i < integralNumber; i += 3) {
                if (i != 0) {
                    c[i++] = ',';
                }
            }        int count = 0;
            for (int i = 0; i < newCharLength; i++) {
                if (c[i] != ',') {
                    c[i] = money.charAt(count++);
                }
            }
            System.out.println("The answer is: ");
            System.out.println(c);
        }
      

  5.   

    public class Test {    public static void main(String[] args) {
            String str = "1234567";
            System.out.println(formatMoney(str));
            str = "1234567.10";
            System.out.println(formatMoney(str));
        }
        
        public static String formatMoney(String money) {
            if(!money.matches("\\d+(\\.\\d{1,2})?")) {
                throw new IllegalArgumentException("money format error!");
            }
            int pointIndex = money.indexOf(".");
            if(pointIndex < 0) {
                money = money + ".00";
            }
            if(pointIndex == money.length() - 2) {
                money = money + "0";
            }
            money = money.replaceAll("(?<=\\d)(?=(?:\\d\\d\\d)+\\.)", ",");
            return money;
        }
    }
      

  6.   

    使用DecimalFormat或者,你有时间,也可以试试NumberFormat 
    同意2楼的做法。
      

  7.   

    money = money.replaceAll("(?<=\\d)(?=(?:\\d\\d\\d)+\\.)", ",");火龙果的正则前面那句(?<=\\d)是做什么的
    可以不要的吧?