Link to home
Start Free TrialLog in
Avatar of vijayam
vijayam

asked on

How can I print a double value 0.0 to 0.00?

Hi

I am trying to print a string value to Double .But i need to print in different format..

I have used the following code

String s1 = "2"
String s2 = ".1"
Double d1 = new Double(s1);
Double d2 = new Double(s2);
System.out.println("First double"+d1+"### Second double "+d2);

Its pirnting
 FirstDouble 2 ### Second double 0.1.

But i want it be printed as
d1 as 2.00 and  d2 as 0.10

How can it be possible?

Thanks.
Vijaya.



Avatar of Mick Barry
Mick Barry
Flag of Australia image

NumberFormat nf = new DecimalFormat("0.00");
s1 = nf.format(d1.doubleValue());
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vijayam
vijayam

ASKER

I am getting the output,but it is as string but i need it as Double.

DecimalFormat df  = new DecimalFormat("0.00");
FieldPosition fp = new FieldPosition (NumberFormat.INTEGER_FIELD);
StringBuffer sb= new StringBuffer();
sb = df.format( d1 , sb, fp );

then its printing sb as 2.00

But i need it to be as Double  that's why  i coverted this to string and parsed..as

Double dd = new Double(sb.toString());

then its printing,same 2 again...what should i do,if i want to print 2.00 as Double object.

thanks.
Vijaya.
The representation of an object, when materialized as a String, has a varying relation to its binary content. In the case of numbers, or object wrappers of numbers, the binary form is, and should be, distinct and different
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial