Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2012 Alejandro P. Revilla
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.BigDecimal;
import java.nio.ByteBuffer;

import java.util.Arrays;

public class Main {

    public static String formatAmountConversionRate(double convRate) {
        if (convRate == 0)
            return null;
        BigDecimal cr = new BigDecimal(convRate);
        int x = 7 - cr.precision() + cr.scale();
        String bds = cr.movePointRight(cr.scale()).toString();
        if (x > 9)
            bds = zeropad(bds, bds.length() + x - 9);
        String ret = zeropadRight(bds, 7);
        return Math.min(9, x) + takeFirstN(ret, 7);
    }

    /**
     * left pad with '0'
     * @param s - original string
     * @param len - desired len
     * @return zero padded string
     * @ if string's length greater than len
     */
    public static String zeropad(String s, int len) {
        return padleft(s, len, '0');
    }

    /**
     * zeropads a long without throwing an ISOException (performs modulus operation)
     *
     * @param l the long
     * @param len the length
     * @return zeropadded value
     */
    public static String zeropad(long l, int len) {
        try {
            return padleft(Long.toString((long) (l % Math.pow(10, len))), len, '0');
        } catch (Exception ignored) {
        }
        return null; // should never happen
    }

    public static String zeropadRight(String s, int len) {
        StringBuilder d = new StringBuilder(s);
        while (d.length() < len)
            d.append('0');
        return d.toString();
    }

    /**
     * return the first n characters of the passed String, left padding where required with 0
     * 
     * @param s
     *            String to take from
     * @param n nuber of characters to take
     * 
     * @return String (may be null)
     */
    public static String takeFirstN(String s, int n) {
        if (s.length() > n) {
            return s.substring(0, n);
        } else {
            if (s.length() < n) {
                return zeropad(s, n);
            } else {
                return s;
            }
        }
    }

    /**
     * pad to the left
     * @param s - original string
     * @param len - desired len
     * @param c - padding char
     * @return padded string
     * @ on error
     */
    public static String padleft(String s, int len, char c)

    {
        s = s.trim();
        if (s.length() > len)
            throw new RuntimeException("invalid len " + s.length() + "/" + len);
        StringBuilder d = new StringBuilder(len);
        int fill = len - s.length();
        while (fill-- > 0)
            d.append(c);
        d.append(s);
        return d.toString();
    }

    public static byte[] padLeft(byte[] tgt, int len, byte padding) {
        if (tgt.length >= len) {
            return tgt;
        }
        ByteBuffer buffer = ByteBuffer.allocate(len);
        byte[] paddings = new byte[len - tgt.length];
        Arrays.fill(paddings, padding);

        buffer.put(paddings);
        buffer.put(tgt);

        return buffer.array();
    }

    /**
     * trim String (if not null)
     * @param s String to trim
     * @return String (may be null)
     */
    public static String trim(String s) {
        return s != null ? s.trim() : null;
    }

    /**
     * Trims a byte[] to a certain length
     * @param array the byte[] to be trimmed
     * @param length the wanted length
     * @return the trimmed byte[]
     */
    public static byte[] trim(byte[] array, int length) {
        byte[] trimmedArray = new byte[length];
        System.arraycopy(array, 0, trimmedArray, 0, length);
        return trimmedArray;
    }
}