Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to Long

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String f = "000000234"

I want to convert this to long with zero's. Please help me
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain your question clearly.

~ abhay
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


above mentioned code gives output as 234. But I think you do not want String to Long conversion to give you value as 234.

~ abhay
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String f = "000000234";
Long la= new Long(f).longValue;
System.out.println(la);

prefix zero's are missing.. I want the zero's
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meet Gaurav wrote:String f = "000000234";
Long la= new Long(f).longValue;
System.out.println(la);

prefix zero's are missing.. I want the zero's


Then keep the value as a String. Some 'numbers' are not actually numbers. Examples are telephone numbers and credit card number which only contain decimal digits but they are not numbers and should not be treated as numbers. One distinguishing question is - do you need to be able to do arithmetic using these 'numbers' ? If the answer is no then don't treat them as numbers; just treat them as Strings.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think , you can get required format in String only.
As soon as you convert it in long, prefix zero will be truncated.

By the way why you require zero prefixed?

~ abhay
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is some code. We badly required that in long
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meet Gaurav wrote:that is some code. We badly required that in long


Why! A long is a number stored in two's compliment binary; note binary and not decimal. It contains no formatting information so once you have parsed a String to a long the resulting long has no knowledge of whether it was derived from a hex string, decimal string, binary string, octal string or via a calculation. You can reconstruct the original string only if you know exactly how many decimal places one started with. An easy way to do this is to format it using String.format() but you do need to know how many decimal places you started with and this is not NOT NOT available from the long.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to realize there is a difference between how a number is stored, and how it is displayed. the valued of the number '2' is the same regardless of whether I write it as "2", "two", "II", or "000000000000000000000000000000000000000000000000000000000000002".

When you store it as an Integer, you are storing the VALUE. you are trying to combine the storing of the value with storing the display - and you can't. it simply doesn't work that way.

If you need the leading 0's, you will have to store it as a string. you MAY be able to do what you want with the printf specifier, but that will generally require you to specify the total number of digits to display.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic