Sign in to follow this  
CRACK123

printing a double value to decimal places in java 1.4

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
Guest
This topic is now closed to further replies.
Sign in to follow this