double in Java example for Android development

Java double is 64 bit double precision type used when fractional
precision calculation is required.

Java double je datový typ (reálné číslo) o velikosti 64 bitů. Používá se například pro přesný výsledek po dělení za desetinnou tečkou. Pokud nepotřebuje tak veliké číslo použijte raději typ float, šetříte tím paměť mobilního telefonu.

		// declaration and assignment of value  type double
		double x = 18.41785;
		//print formated  value
		System.out.printf("The value of x is %.3f%n", x); // 18.418
 
                // declaring more variables in single statement
                double d1 = 12.4, d2 = 564.5, d3 = 14.589;
 
		// double range of value
		System.out.println(Double.MIN_VALUE); // 4.9E-324
		System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
 
		// is NaN  Not-a-Number
		double f = (double) Math.sqrt(-15);
		boolean bNaN = Double.isNaN(f);
		System.out.print(bNaN); // true
 
                // check if a string is a valid number in Java example
                // convert string to double Java example
		String sD = "12.8";
		double dParse = Double.parseDouble(sD);
 
	       // convert strings to numbers
	      String sDl = "15.48";
	      double dFromString = (Double.valueOf(sDl)).doubleValue();
 
// format double, float or long value to string
	DecimalFormat formatter = new DecimalFormat(".##");
	String s = formatter.format(-.5678);             // -0.57
// .###  -0.568
// .#### -0.5678
// .000000  -.567800
 
// -123.456
// .##  -123.46
// #.##  -123.46
 
// #E0  -.1E3
// ##E0  -1.2E2
//###E0  -123E0
 
// double to string in Java example code
Double dObj = new Double(68.5);
String str = dObj.toString();
// else 
Double dS = 11.6;
String sdouble = dS.toString();
 
// compare two double variables
	      Double dComp1 = 4.3;
	      if(dComp1.equals(4.3))
	  		System.out.print("true");
 
	// compares the two specified double values in Java example
	// int i =	compare(double d1, double d2);
	    int i =	Double.compare(11.5, 11.7); // -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.



8,083 views