944,134 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6409
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 20th, 2007
0

convert string to double, and result is string again

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
onsir is offline Offline
38 posts
since Apr 2007
Aug 20th, 2007
0

Re: convert string to double, and result is string again

I'm quite sure 5 mins of google will solve this problem for you.
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Aug 20th, 2007
0

Re: convert string to double, and result is string again

please give me a link, thanks.
Reputation Points: 10
Solved Threads: 1
Light Poster
onsir is offline Offline
38 posts
since Apr 2007
Aug 20th, 2007
0

Re: convert string to double, and result is string again

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);


}
}
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Aug 20th, 2007
0

Re: convert string to double, and result is string again

Awwwwwwwww, but how do I get the leading zeroes back?! Will be his next post.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Aug 21st, 2007
0

Re: convert string to double, and result is string again

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<leng;j++)
{
char c=code.charAt(j);
if(c=='0')
{
cnt++;

}
else
{
break;
}
System.out.println(cnt);
}
String si="";
for (int k=0;k<cnt ;k++ )
{
si=si+"0";
}
double dbl=Double.parseDouble(code);
double dbl1=dbl+1;

String result=si+dbl1+"";

System.out.println(result);


}
}
Last edited by vinod_javas; Aug 21st, 2007 at 2:59 am.
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Aug 21st, 2007
0

Re: convert string to double, and result is string again

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
Reputation Points: 9
Solved Threads: 1
Light Poster
satish.paluvai is offline Offline
45 posts
since Mar 2007
Aug 21st, 2007
0

Re: convert string to double, and result is string again

Repeat reply #5.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Aug 26th, 2007
0

Re: convert string to double, and result is string again

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.

Java Syntax (Toggle Plain Text)
  1. int i = 1;
  2. System.out.printf("%010d%n", i);
  3. i += 10;
  4. System.out.printf("%010d%n", i);
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 26th, 2007
0

Re: convert string to double, and result is string again

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: calling another function in nested class
Next Thread in Java Forum Timeline: Need Help, Getting Exceptions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC