Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Converting double to string in 7e100 format

Reply
Thread Tools

Converting double to string in 7e100 format

 
 
Pudu
Guest
Posts: n/a
 
      06-18-2004
I have a double value 17435340000000000000. I want to convert it to
1.743534e20 and it should be a string value. Any help would be
appreciated.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-18-2004
On 18 Jun 2004 15:42:36 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Pudu) wrote or
quoted :

>I have a double value 17435340000000000000. I want to convert it to
>1.743534e20 and it should be a string value. Any help would be
>appreciated.


see http://mindprod.com/jgloss/converter.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Liz
Guest
Posts: n/a
 
      06-18-2004
in 1.5 sprintf()


"Pudu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have a double value 17435340000000000000. I want to convert it to
> 1.743534e20 and it should be a string value. Any help would be
> appreciated.



 
Reply With Quote
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      06-19-2004
Pudu wrote:

> I have a double value 17435340000000000000. I want to convert it to
> 1.743534e20 and it should be a string value. Any help would be
> appreciated.


Peace be unto you.

import java.text.DecimalFormat;
class TestingAndQualityAssuranceDepartment2
{
public static void main(String [] args)
{
double x = 17435340000000000000d;
DecimalFormat df = new DecimalFormat("0.000000E00");
System.out.println(df.format(x).toLowerCase());
}
}

>java TestingAndQualityAssuranceDepartment2

1.743534e19
>Exit code: 0



Have a good day.
 
Reply With Quote
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      06-19-2004
Liz wrote:

> in 1.5 sprintf()


Peace.



import java.text.DecimalFormat;
import java.util.Locale;
import java.util.Formatter;
import java.text.MessageFormat;
class TestingAndQualityAssuranceDepartment3
{
public static void main(String [] args)
{
double x = 0.0000000000001743534d;
System.out.printf("%1$+e\n", x);
System.out.println(Double.toString(x).toLowerCase( ));
StringBuilder sb = new StringBuilder(); // Appendable
Formatter formatter = new Formatter(sb, Locale.CANADA);
formatter.format("%e", x);
System.out.printf("%s",sb.toString());
Object [] obj = {new Double(x)};
MessageFormat mf = new MessageFormat("\n{0,number,0.000000E00}");
System.out.printf("%s\n", mf.format(obj));
DecimalFormat df = new DecimalFormat("0.000000E00");
System.out.println(df.format(x).toLowerCase());
}
}

"C:\Program Files\Java\jdk1.5.0\bin\javac" -source 1.5
TestingAndQualityAssuranceDepartment3.java
"C:\Program Files\Java\jdk1.5.0\bin\java.exe" -version:1.5
TestingAndQualityAssuranceDepartment3
+1.743534e-13
1.743534e-13
1.743534e-13
1.743534E-13
1.743534e-13



%[argument_index$][flags][width]conversion
You can get away with just %e - e being the conversion;
or %1$e - $1 being the argument_index
or %1$+e - + being a flag to indicating The result will always include
a sign
sprintf?

Have a good day.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-20-2004
> "Pudu" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I have a double value 17435340000000000000. I want to convert it to
> > 1.743534e20 and it should be a string value. Any help would be
> > appreciated.


Liz wrote:
> in 1.5 sprintf()


Perhaps it's time to start thinking about what's a good time to use
PrintWriter's new printf methods. I see this as a definite border case,
because the goal is a single String conversion of a data type. If the
goal were to print lines formatted in a specific way while plugging in
some set of multiple values, then the situation would be different.

Is there a good reason to do this with PrintWriter.printf, instead of
with DecimalFormat?

Incidentally, I don't see a sprintf per se in the 1.5 API docs. Am I
missing it? If it's PrintWriter.printf you're referring to, then this
involves even more added complexity of creating a StringWriter to wrap
in the PrintWriter, and then retrieving the result.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      06-22-2004

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)4.net...
> > "Pudu" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) om...
> > > I have a double value 17435340000000000000. I want to convert it to
> > > 1.743534e20 and it should be a string value. Any help would be
> > > appreciated.

>
> Liz wrote:
> > in 1.5 sprintf()

>
> Perhaps it's time to start thinking about what's a good time to use
> PrintWriter's new printf methods. I see this as a definite border case,
> because the goal is a single String conversion of a data type. If the
> goal were to print lines formatted in a specific way while plugging in
> some set of multiple values, then the situation would be different.
>
> Is there a good reason to do this with PrintWriter.printf, instead of
> with DecimalFormat?


It's easy and it's free.

>
> Incidentally, I don't see a sprintf per se in the 1.5 API docs. Am I
> missing it? If it's PrintWriter.printf you're referring to, then this
> involves even more added complexity of creating a StringWriter to wrap
> in the PrintWriter, and then retrieving the result.


I didn't look really, since I'm not going to use 1.5 until it is official.
(i.e. not one of those greek names like hans beta)
>
> --
> www.designacourse.com
> The Easiest Way to Train Anyone... Anywhere.
>
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation



 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-22-2004
Liz wrote:
> > Is there a good reason to do this with PrintWriter.printf, instead of
> > with DecimalFormat?

>
> It's easy and it's free.


And yet DecimalFormat provides the same service, and is equally easy (as
far as I can tell) and certainly equally free...

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      04-15-2006
Hello, Liz!
You wrote:

> in 1.5 sprintf()


1.5 will indeed have a printf/sprintf type of functionality, but
it is not actually called sprintf. IIRC there is a class called
formatter that provides the printf functionality and String will
gain a format method to do the equivalent of sprintf.
--
Dale King
My Blog: http://daleking.homedns.org/Blog
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: raw format string in string format method? Chris Angelico Python 3 03-01-2013 12:00 AM
Number format exception while converting from double to int =?iso-8859-1?B?bW9vcJk=?= Java 3 01-23-2006 08:05 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Help with Function - converting a double num into a hh:mm:ss format D. Shane Fowlkes ASP .Net 2 12-16-2004 06:42 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments