This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# String to Long

Meet Gaurav
Ranch Hand
Posts: 492
String f = "000000234"

Abhay Agarwal
Ranch Hand
Posts: 1376

~ abhay

Abhay Agarwal
Ranch Hand
Posts: 1376

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
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
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
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
that is some code. We badly required that in long

James Sabre
Ranch Hand
Posts: 781
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: 12097
30
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.