提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
比较浮点类型值
public class NumberDemo {/**来 自 时代Java公众号 - N o w J a v a . c o m**/ public static void main(String args[]) { Float floatOne = new Float(14.78f - 13.78f); Float floatTwo = Float.valueOf("1.0"); Double doubleOne = new Double(1.0); int difference = floatOne.compareTo(floatTwo); if (difference == 0) { System.out.println("floatOne is equal to floatTwo."); } else if (difference < 0) { System.out.println("floatOne is less than floatTwo."); } else if (difference > 0) { System.out.println("floatOne is greater than floatTwo."); } System.out.println("floatOne is " + ((floatOne.equals(doubleOne)) ? "equal" : "not equal") + " to doubleOne."); } }/**时 代 Java 公 众 号 - nowjava.com**/