wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String to Long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to Long" Watch "String to Long" New topic
Author

String to Long

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
String f = "000000234"

I want to convert this to long with zero's. Please help me
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1075
    
    1

please explain your question clearly.

~ abhay


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1075
    
    1



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

Joined: Oct 08, 2008
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

Joined: Sep 07, 2004
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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1075
    
    1

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

Joined: Oct 08, 2008
Posts: 492
that is some code. We badly required that in long
James Sabre
Ranch Hand

Joined: Sep 07, 2004
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

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to Long
 
Similar Threads
Converting String to long
convert a String to Long
Iteration in XP
array store exception
StringOutOfBoundsException