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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>when i converted it to from string to long

Leading zeros are attributes of strings, not numbers
chaitu chaituAuthor Commented:
as u said it returns in string format but i want to assign this value to long varaible;
CEHJCommented:
The code you posted already  does that
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

chaitu chaituAuthor Commented:
but i am losing 0 precedence if i do this  Long.parseLong(x)
WebstormCommented:
long l=Long.parseLong(x);

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

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

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

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

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

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

Then the question of whether it has leading zeros is meaningless
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;
WebstormCommented:
->
String x ="0xxxxxxx";
Emp.refno =x;
chaitu chaituAuthor Commented:
how can u assign string to long varaible???/
WebstormCommented:
then:
Emp.refno =Long.parseLong(x);
chaitu chaituAuthor Commented:
then leading zero will not come in Emp.refno ;;;
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
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
chaitu chaituAuthor Commented:
then leading zero will not come in Emp.refno

I need the leading zero



WebstormCommented:
-> use String instead of long

if you need both long and leading '0' -> impossible
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);
chaitu chaituAuthor Commented:
toLongValue()????
chaitu chaituAuthor Commented:
Error(5,28): can't inherit from final class java.lang.Long
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);
    }
}
WebstormCommented:
RefNo refno=new RefNo(707L);
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chaitu chaituAuthor Commented:
yes i understood long before.....
CEHJCommented:
>>yes i understood long before.....

Then the question is solved
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.