Java Long Number Format formatLong(long value, int length)

Here you can find the source of formatLong(long value, int length)

Description

Format long value

License

Open Source License

Parameter

Parameter Description
value a parameter
length a parameter

Return

string

Declaration

private static String formatLong(long value, int length) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  w  w  .  jav  a 2 s  .  com
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Format long value
     * 
     * @param value
     * @param length
     * @return string
     */
    private static String formatLong(long value, int length) {
        boolean isPostive = value >= 0;
        value = isPostive ? value : value * -1;

        String result = formatStr2("" + value, length - 1);
        if (isPostive)
            result = " " + result;
        else
            result = "-" + result;

        return result;
    }

    /**
     * Add space char to the beginning of string
     * 
     * @param inputStr
     * @param length
     * @return string
     */
    private static String formatStr2(String inputStr, int length) {
        return formatStr(inputStr, length, false);
    }

    /**
     * Format string, add space char to the string
     * @param inputStr
     * @param length
     * @param appendToTail
     * @return string
     */
    private static String formatStr(String inputStr, int length, boolean appendToTail) {
        if (inputStr == null)
            return null;

        int inputLen = inputStr.length();
        if (inputLen >= length)
            return inputStr;

        int appendLen = length - inputLen;
        char[] appendChar = new char[appendLen];
        for (int i = 0; i < appendLen; i++) {
            appendChar[i] = ' ';
        }

        String result;
        if (appendToTail == true)
            result = inputStr + new String(appendChar);
        else
            result = new String(appendChar) + inputStr;

        return result;
    }
}

Related

  1. formatLong(long inLong, int inLen, boolean inComma, int inCommaPos)
  2. formatLong(Long number)
  3. formatLong(long val, int size)
  4. formatLong(long value)
  5. formatLong(long value)
  6. formatLong(String value)
  7. formatLongAsDecimal(long l)
  8. longToSimpleString(long value)