Archived

This topic is now archived and is closed to further replies.

wolfbane

[java] working with floats

Recommended Posts

wolfbane    122
Does anyone know how do you cut the float point decimal places to the hundreth place? how do I get 1.23999999999 to be 1.24?

Share this post


Link to post
Share on other sites
Loves_Java    122
You have to use the DecimalFormat.
Here is an example


    
import java.text.DecimalFormat; // Import DecimalFormat

public class it
{
public static void main(String args[])
{
// Used to set the decimalFormat to two decimal places

DecimalFormat twoDigits = new DecimalFormat ("0.00");

float num = 1.23999999999999;

// Print out the number and force it to two decimal places

System.out.println(twoDigits.format(num));
}
}


Hope this helps

[edited by - Loves_Java on May 1, 2002 6:17:54 PM]

Share this post


Link to post
Share on other sites