Converting double to string in 7e100 format

P

Pudu

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.
 
M

mromarkhan

Pudu said:
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.
 
M

mromarkhan

Liz said:
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.
 
C

Chris Smith

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
 
L

Liz

Chris Smith said:
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)
 
C

Chris Smith

Liz said:
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
 
D

Dale King

Hello, Liz!
You said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top