converted it to from string to long precedence 0 should be added

String x="07070818";
    System.out.println(Long.parseLong(x));

when i converted it to from string to long precedence 0 should be added.
LVL 20
chaitu chaituAsked:
Who is Participating?
 
CEHJCommented:
chaituu - there seems to be a fundamental misunderstanding:

numbers do NOT have leading zeros and such a concept is meaningless. Only graphical representations of numbers (strings) are given leading zeros for formatting purposes. For the purposes of entering the number into the db, leading zeros should be ignored. When it comes to *showing* the db value as a string, that's a different matter and you've been given several different ways to do that
0
 
CEHJCommented:
>>when i converted it to from string to long

Leading zeros are attributes of strings, not numbers
0
 
chaitu chaituAuthor Commented:
as u said it returns in string format but i want to assign this value to long varaible;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
CEHJCommented:
The code you posted already  does that
0
 
chaitu chaituAuthor Commented:
but i am losing 0 precedence if i do this  Long.parseLong(x)
0
 
WebstormCommented:
long l=Long.parseLong(x);

then reformat when you display
0
 
WebstormCommented:
then reformat when you display or convert to a string :

String s=String.format("%08d", l );
0
 
CEHJCommented:
>>then reformat when you display

You don't need to - it was derived  from a leading zero string in the first place ;-)
0
 
chaitu chaituAuthor Commented:
can i use NumberFormat in this case;;
0
 
chaitu chaituAuthor Commented:
my aim is return value should be in long ;
0
 
WebstormCommented:
>> can i use NumberFormat in this case;;
yes
>> my aim is return value should be in long
then you won't have leading 0, and should format it when you display its value.
0
 
chaitu chaituAuthor Commented:
"then you won't have leading 0, and should format it when you display its value"

How?
0
 
CEHJCommented:
System.out.printf("%08d\", values);
0
 
CEHJCommented:
Typo - should be

 System.out.printf("%08d\n", value);
0
 
chaitu chaituAuthor Commented:
Error(18,15): method printf(java.lang.String, java.lang.String) not found in class java.io.PrintStream
0
 
CEHJCommented:
You will need to use NumberFormat as you have an old version of Java
0
 
WebstormCommented:
NumberFormat nf=new NumberFormat();
nf.setMinimumIntegerDigits(8);
String s=nf.format(l);  
0
 
chaitu chaituAuthor Commented:
problem still persists


  String x="0707";
  DecimalFormat fmt = new DecimalFormat("#########0000");

       
        Number n = fmt.parse(x);
       
          String strDoubleValue=fmt.format(n.longValue());
          System.err.println("strDoubleValue :"+strDoubleValue);  --0707
          long doubleValue1=Long.parseLong(strDoubleValue);
          System.err.println("doublevalue :"+doubleValue1);----------707
0
 
WebstormCommented:
long is only a binary primitive type, and will old your value as binary : 707
displaying such type discard any leading 0.
-> need to format as a string

   
0
 
WebstormCommented:
If you want to keep leading 0, use String
0
 
chaitu chaituAuthor Commented:
i want to assign this long value to some other long varaible
0
 
CEHJCommented:
>> want to assign this long value to some other long varaible

Then the question of whether it has leading zeros is meaningless
0
 
chaitu chaituAuthor Commented:
my requirment is i want the format with leading zeros say x ="0xxxxxxx" this value should be inserted into database;

 i will assign this  long varaible  say Emp.refno =x somewhere in controller;
0
 
WebstormCommented:
->
String x ="0xxxxxxx";
Emp.refno =x;
0
 
chaitu chaituAuthor Commented:
how can u assign string to long varaible???/
0
 
WebstormCommented:
then:
Emp.refno =Long.parseLong(x);
0
 
chaitu chaituAuthor Commented:
then leading zero will not come in Emp.refno ;;;
0
 
chaitu chaituAuthor Commented:
String x="0707";
  DecimalFormat fmt = new DecimalFormat("#########0000");

       
        Number n = fmt.parse(x);
       
          String strDoubleValue=fmt.format(n.longValue());
          System.err.println("strDoubleValue :"+strDoubleValue);  ----- 0707
       
long x1 =Long.parseLong(strDoubleValue);
System.out.println(x1);----------707
0
 
WebstormCommented:
>> then leading zero will not come in Emp.refno
because you don't need it

if you want to display it -> format
OR
if you want to store it in DB with '0' -> use String instead of long
0
 
chaitu chaituAuthor Commented:
then leading zero will not come in Emp.refno

I need the leading zero



0
 
WebstormCommented:
-> use String instead of long

if you need both long and leading '0' -> impossible
0
 
WebstormCommented:
but you can use Long, and extend the class :

public class RefNo extends Long
{
   DecimalFormat fmt = new DecimalFormat("#########0000");
   public String toString()
   {
        long l=toLongValue();
        return fmt.format(l);
    }
}

Long refno=new RefNo(707L);
0
 
chaitu chaituAuthor Commented:
toLongValue()????
0
 
chaitu chaituAuthor Commented:
Error(5,28): can't inherit from final class java.lang.Long
0
 
WebstormCommented:
public class RefNo
{
   long refno;
   public RefNo(long l){ this.refno=l; }
   DecimalFormat fmt = new DecimalFormat("#########0000");
   public String toString()
   {
        long l=refno
        return fmt.format(l);
    }
}
0
 
WebstormCommented:
RefNo refno=new RefNo(707L);
0
 
chaitu chaituAuthor Commented:
yes i understood long before.....
0
 
CEHJCommented:
>>yes i understood long before.....

Then the question is solved
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.