954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

convert string to double, and result is string again

hello
how to convert string to double, and result is string.
example ;
String code =00000000001;
Double plus =code + 1;
String result =plus; // and result is 0000000002

thanks

onsir
Light Poster
38 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

I'm quite sure 5 mins of google will solve this problem for you.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

please give me a link, thanks.

onsir
Light Poster
38 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

Have this as example.......

class doubconv
{
public static void main(String args[])
{
String code="00000000001";
double dbl=Double.parseDouble(code);
double dbl1=dbl+1;
String result=dbl1+"";

System.out.println(result);

}
}

vinod_javas
Practically a Posting Shark
871 posts since Feb 2007
Reputation Points: 119
Solved Threads: 7
 

Awwwwwwwww, but how do I get the leading zeroes back?! Will be his next post.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

if you have better way more than this means please show your solutions also. it will be really useful

(for adding zero)


class doubconv
{
public static void main(String args[])
{
String code="00000000001";
int leng=code.length();
int cnt=0;
for(int j=0;j

vinod_javas
Practically a Posting Shark
871 posts since Feb 2007
Reputation Points: 119
Solved Threads: 7
 

hello,
String s1=new String("0000001");
Double d=new Double(s1);
double d1=d.doubleValue();
s1=d1+"";
if u solve ur problem plz give a reply. i hopw u will solve ur problem by using this simple logic

satish.paluvai
Light Poster
45 posts since Mar 2007
Reputation Points: 9
Solved Threads: 1
 

Repeat reply #5.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I am almost sure that you are doing all this for display purposes. If so, then why not just use the formatting provided by the printf method.

int i = 1;
System.out.printf("%010d%n", i);
i += 10;
System.out.printf("%010d%n", i);
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

That or use NumberFormat.
To any sane person (and a computer in that regard is a sane person) the leading zeros are utterly unimportant, a darned nuisance even, and thus stripped and ignored.

None of you except sos realised that apparently. None of you took the trouble to actually look at how numbers are represented in a computer, and thus realised that there are no leading zeros anywhere.
The number is stored as a series of bits representing a number, NOT a string, and a number never has leading zeros.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

That or use NumberFormat. To any sane person (and a computer in that regard is a sane person) the leading zeros are utterly unimportant, a darned nuisance even, and thus stripped and ignored.

None of you except sos realised that apparently. None of you took the trouble to actually look at how numbers are represented in a computer, and thus realised that there are no leading zeros anywhere. The number is stored as a series of bits representing a number, NOT a string, and a number never has leading zeros.

Obviously my posts were facetious. This idoicy of storing a double as a string and converting back and forth to do math with it is just that, idoicy.Obviously you simply use (and store) a double as a double and format the output as you want it, when it comes time to produce output, and not before. Anything else, is, as already stated, idiocy.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You