TechFundu

  • Increase font size
  • Default font size
  • Decrease font size
Welcome Guest,  Login | Join Now
Home Groups Core Java Developers Discussions How do you left pad or right pad a number in java?
How do you left pad or right pad a number in java?
Discussion started by Deepika Negi , on 11 January 12:17 PM
I have a requirement where I have to left pad and right pad numbers using leading and trailing zeros. Let me explain my requirement with an example. Suppose some field strike price value is 123.5, here whole number has to be always 5 digits (e.g. 00123 here) and decimal number always 3 digits (e.g. 500 here) so I have to left pad and right pad these number if they are less than 5 digits for whole number and less than 3 digits for decimal number.

Any idea how to achieve this? I am using JDK1.5 for development...
Replies
You will need to be a member of this group first before you can post a reply.
Raghuveer Rawat
I missed a point here.. You can easily do this using below code..

String strStrkiePrice = "123.5";

DecimalFormat df = new DecimalFormat("00000.000");
strStrkiePrice = df.format(new Double(strStrkiePrice));
int dot = strStrkiePrice.indexOf(".");

//Strike Price Whole
String strikePriceWhole = strStrkiePrice.substring(0,dot);
//Result: 00123

//Strike Price Decimal
String strikePriceDecimal = strStrkiePrice.substring(dot+1);
//Result: 500
Tuesday, 19 January 2010 10:30
 
Raghuveer Rawat
Hi Deepika,

Unfortunately, StringUtils class of Apache Commons Lang seems to be easiest way to left and right pad a string with specified character.

http://commons.apache.org/lang/apidocs/org/apache/commons/lang/StringUtils.html

Use below methods:

Left pad a String with a specified character.
static String leftPad(String str, int size, char padChar)

Right pad a String with a specified character.
static String rightPad(String str, int size, char padChar)

You can easily left pad a number or String with zeros using String.format(String format, Object... args) or DecimalFormat but I could not find any solution for right padding with zeros. May be I am missing something, but would love to hear if any other java developer has solution for right padding number or string with zeros.

e.g. String.format("%1$05d", 123) returns '00123' or you can create DecimalFormat like new DecimalFormat("#####") and apply to any number but both of them do left padding.

If you can use apache lang for some reason then you can left padding using String.format and use below methods for right padding. This will solve your problem.

private static String padding(int repeat, char padChar) throws IndexOutOfBoundsException {
if (repeat < 0) {
throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);
}
final char[] buf = new char[repeat];
for (int i = 0; i < buf.length; i++) {
buf[i] = padChar;
}
return new String(buf);
}

public static String rightPad(String str, int size, String padStr) {
if (str == null) {
return null;
}
if (isEmpty(padStr)) {
padStr = " ";
}
int padLen = padStr.length();
int strLen = str.length();
int pads = size - strLen;
if (pads <= 0) {
return str; // returns original String when possible
}
if (padLen == 1 && pads <= PAD_LIMIT) {
return rightPad(str, size, padStr.charAt(0));
}

if (pads == padLen) {
return str.concat(padStr);
} else if (pads < padLen) {
return str.concat(padStr.substring(0, pads));
} else {
char[] padding = new char[pads];
char[] padChars = padStr.toCharArray();
for (int i = 0; i < pads; i++) {
padding[i] = padChars[i % padLen];
}
return str.concat(new String(padding));
}
}

public static String rightPad(String str, int size, char padChar) {
if (str == null) {
return null;
}
int pads = size - str.length();
if (pads <= 0) {
return str; // returns original String when possible
}
if (pads > PAD_LIMIT) {
return rightPad(str, size, String.valueOf(padChar));
}
return str.concat(padding(pads, padChar));
}

public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}

Tuesday, 12 January 2010 13:24
 

TechFundu Polls

Which web browser do you like most?

Follow us on Twitter