float in Java example code

Android development
Java float is 32 bit single precision type and used when fractional precision calculation is required.

Java float je 32 bitů veliké číslo sloužící především pro přesný výsledek za desetinnou tečkou například při dělení čísel. Pro větší přesnost použíijte 64 bitový typ Double.

		// declaration and assignment of value  type float
		float x = 18.41785f;
		//print formated  value
		System.out.printf("The value of x is %.3f%n", x); // 18.418
 
                // declaring more variables in single statement
                float f1 = 12.4F, f2 = 564.5F, f3 = 14.589F;
 
		// float range of value
		System.out.println(Float.MIN_VALUE); // 4E-45
		System.out.println(Float.MAX_VALUE); // 4028235E38
 
		// is NaN  Not-a-Number
		float f = (float) Math.sqrt(-15);
		boolean bNaN = Float.isNaN(f);
		System.out.print(bNaN); // true
 
                // check if a string is a valid number in Java example
                // convert string to float Java example
		String sF = "12.8";
		float fParse = Float.parseFloat(sF);
 
	       // convert strings to numbers
	      String sFl = "15.48";
	      float fFromString = (Float.valueOf(sFl)).floatValue();
 
// float to string in Java example code
Float fObj = new Float(68.5);
String str = fObj.toString();
// else 
Float fS = 11.6f;
String sFloat = fS.toString();
 
              // compare two float variables
	      Float fComp1 = 4.3f;
	      if(fComp1.equals(4.3f))
	  		System.out.print("true");
 
	    // compares the two specified float values in Java example
	    int i =	Float.compare(11.5f, 11.7f); // -1 first < second
	    // 0 first == second
	    // 1 first > second
	    System.out.print(i);
This entry was posted in Android Examples Code and tagged , , . Bookmark the permalink.


Comments are closed.



13,977 views