Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.text.DecimalFormat;

public class Main {
    public static String changeMoney(String pattern, String value) {
        double va = 0;
        try {
            va = Double.parseDouble(value);
        } catch (Exception e) {
            return "";
        }
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        myFormatter.applyPattern(pattern);
        return myFormatter.format(va);
    }

    public static String format(String format, Object targetObc) {
        String target = String.valueOf(targetObc);
        if (format != null && !format.equals("")) {
            if (format.contains("#")) {
                String head = format.substring(0, format.indexOf("#"));
                String body = format.substring(format.indexOf("#"));
                //String tail=format.substring(format.lastIndexOf("#")+1);
                StringBuilder result = new StringBuilder(head);
                int j = 0;
                for (int i = 0; i < target.length(); i++) {
                    if (j > body.length() - 1) {
                        j = 0;
                    }
                    String s = String.valueOf(body.charAt(j));
                    if ("#".equals(s)) {
                        result.append(String.valueOf(target.charAt(i)));
                    } else {
                        result.append(String.valueOf(body.charAt(j)));
                        //result.append(String.valueOf(target.charAt(i)));
                        i--;
                    }
                    j++;
                }
                return result.toString();
            } else {
                return target;
            }
        } else {
            return target;
        }
    }
}