How to compare two primitive float values in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {
 
    public static void main(String[] args) {
  
        float f1 = 3.1415f;
        float f2 = 3.1414f;
        int n = Float.compare(f1, f2);

        if (n > 0) {
            System.out.println("f1 > f2");
        } else if (n < 0) {
            System.out.println("f2 > f1");
        } else {
            System.out.println("f1 = f2");
        }
    }
}
 
/*
    
run:

f1 > f2

*/

 



answered Nov 4, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {
 
    public static void main(String[] args) {
  
        float f1 = 3.1415f;
        float f2 = 3.1414f;

        if (f1 > f2) {
            System.out.println("f1 > f2");
        } else if (f2 > f1) {
            System.out.println("f2 > f1");
        } else {
            System.out.println("f1 = f2");
        }
    }
}
 
/*
    
run:

f1 > f2

*/

 



answered Nov 4, 2016 by avibootz

Related questions

2 answers 189 views
1 answer 192 views
192 views asked Sep 23, 2016 by avibootz
1 answer 208 views
1 answer 159 views
1 answer 140 views
1 answer 145 views
1 answer 237 views
...