Here you can find the source of formatLong(long value, int length)
Parameter | Description |
---|---|
value | a parameter |
length | a parameter |
private static String formatLong(long value, int length)
//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; } }