Here is my code
Integer.pasrseInt(new DecimalFormat(0000).format(230));
This returns a String
but I want to save it as an Integer
.
Thanks in advance.
Here is my code
Integer.pasrseInt(new DecimalFormat(0000).format(230));
This returns a String
but I want to save it as an Integer
.
Thanks in advance.
Maybe it's an overkill but maybe this give you an idea of why you cannot directly do what you asked:
public class MyInteger {
private Integer value;
public MyInteger(Integer value){
this.value = value;
}
public String showWithPad(){
return DecimalFormat(0000).format(value);
}
}
Then you do
MyInteger myInteger = new MyInteger(230);
System.out.println(myInteger.showWithPad());
And this prints
0230
As it was stated you cannot pad an Integer
but its representation (eg the way you show it). Then you have to store the number as is (an Integer
) and add the padding zeros when you want to represent it this way.
The best way it that you can prepend
0
s if the characters are less than 4. Integers cannot have0
in front. AFAIK, onlyString
objects can be created by prepending0
s to integers. Note, when you convert theString
back toInteger
, the prepended zeros will be removed.