Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 // This software is subject to the terms of the Eclipse Public License v1.0
 // Agreement, available at the following URL:
 // http://www.eclipse.org/legal/epl-v10.html.
 // You must accept the terms of that agreement to use this software.
 //
 // Copyright (C) 2003-2005 Julian Hyde
 // Copyright (C) 2005-2011 Pentaho
 // Copyright (c) 2008-2014 Open Link Financial, Inc. All Rights Reserved.
 */

import java.text.NumberFormat;
import java.util.*;

public class Main {
    /**
     * Corrects for the differences between numeric strings arising because
     * JDBC drivers use different representations for numbers
     * ({@link Double} vs. {@link java.math.BigDecimal}) and
     * these have different toString() behavior.
     *
     * <p>If it contains a decimal point, then
     * strip off trailing '0's. After stripping off
     * the '0's, if there is nothing right of the
     * decimal point, then strip off decimal point.
     *
     * @param numericStr Numeric string
     * @return Normalized string
     */
    public static String normalizeNumericString(String numericStr, boolean isDecimal) {

        if (numericStr.equals("0") || numericStr.equals(null))
            return "0";

        //trim the ".0" if the input integer is in the format ##.0 
        if (!isDecimal && numericStr.contains(".")) {
            String[] strArray = numericStr.split("\\.");
            return strArray[0];
        }

        int index = numericStr.indexOf('.');

        //limit the number of decimal of numericStr to 5
        if (numericStr.length() > index + 4) {
            numericStr = numericStr.substring(0, index + 4);
        }

        //    return numericStr;
        char[] chars = numericStr.toCharArray();
        int start = 0;
        if (chars[0] == '-') {
            start = 1;
        }

        Integer E = 0;
        for (int i = start; i < chars.length; i++) {
            if (chars[i] == '.') {
                E = i - 1;
                while (i > 1) {
                    chars[i] = chars[i - 1];
                    i--;
                }
                chars[start + 1] = '.';
                break;
            }
        }
        return new String(chars).concat("E" + E.toString());
    }

    /**
     * Normalize decimal string
     * exclude the Expo
     * @param numericStr
     * @param isDecimal
     * @param digits
     * @return
     */
    public static String normalizeNumericString(String numericStr, boolean isDecimal, int digits) {

        if (isDecimal) {
            digits += 1;
            Double d = new Double(numericStr);
            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMinimumFractionDigits(7);
            numericStr = nf.format(d);

            int dotIndex = numericStr.indexOf(".");
            if (numericStr.length() <= (dotIndex + digits))
                return numericStr;
            String result = numericStr.substring(0, dotIndex + digits);
            result = result.replace(",", "");
            return result;
        }
        throw new RuntimeException();
    }

    private static <T> String toString(List<T> list) {
        StringBuilder buf = new StringBuilder();
        int k = -1;
        for (T t : list) {
            if (++k > 0) {
                buf.append(", ");
            }
            buf.append(t);
        }
        return buf.toString();
    }
}