1.11M Members

how use integer values begins with zero

 
0
 

Hi all,

I want to set an integer variable that is begins with 0 (ex: 01, 055 etc).

So I don’t want to use strings here, because I want to use this values as mathematical values..

What is the data type that i have to use here..? So please anyone can help me..

Thanks...
janaka priyadarshana

 
0
 

just get it as string..... and then u can convert that value in to integer
like this

String xyz="";

si=Integer.parseInt(xyz);


Its just my idea....if better solutions are there means most welcome...

 
0
 

You cannot store an integer value as an integer, and with a leading zero. I assume you only need the leading zero when printing the number, right?

Than look into the format method for String, if you are using at least 1.5. (And follow the "format string" link found in the method description for format.

 
0
 

yap.. i want it to display with leading zero..but all the numbers do not begins with zero..

so now what should i do....?

 
0
 

while printing only..if you want that leading zero means..i think only the way you should use string... and after that you want it as integer value means just parse it as integer...


try like this....

public class strtes
{
public static void main(String args[])
{
int x=12;
int y=10;
String a="0"+x;
String b="0"+y;
int z=Integer.parseInt(a);
int z1=Integer.parseInt(b);
System.out.println(a);
System.out.println(b);
System.out.println(z);
System.out.println(z1);
}
}

 
0
 

yap.. i want it to display with leading zero..but all the numbers do not begins with zero..
so now what should i do....?

I would do all my calculations and just befor displaying detect which number requeres leading zeros and how many.

 
0
 

I assume the number should have a specific length front padded with zeros, right? i.e.

001
002
055
100

Well, then as I said, follow the link for "format string". You call tell format that you want the number to be three digits long and front padded with zeros. Then you do not have to decide when to pad, format will.

So, I need to ask, did you actually read what I told to read, and attempt to figure it out, or do you just want "teh codes" (mispelled on purpose).

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article