Sign in to follow this  

printing a double value to decimal places in java 1.4

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, How do I print a value to 2 decimal places in java if the value is a type of double I tried DecimalFormat by doing
String str = Double.toString(value);
DecimalFormat dec = new DecimalFormat(str);
dec.setMaximumFractionDigits(2);
String res = dec.toString();


But this does not seem to work. Thanks

Share this post


Link to post
Share on other sites

import java.text.*;

public class Test
{
public static void main(String[] args)
{
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
}
}

Share this post


Link to post
Share on other sites

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this