Search This Blog

Featured Post

Machine Learning, Big Data, AI, Deep Learning

Wednesday, April 17, 2019

Java Number & Math

Math in Java


Input

public class test{
public static void main(String[] args){

System.out.println("sin:" + Math.sin(Math.PI/2));
System.out.println("cos:" + Math.cos(0));
System.out.println("tan:" + Math.tan(Math.PI/3));
System.out.println("atan:" + Math.atan(1));
System.out.println("toDegrees:" + Math.toDegrees(Math.PI/2));
System.out.println(Math.PI);
}

}

Output
D:\javafiles>java test.java
sin:1.0
cos:1.0
tan:1.7320508075688767
atan:0.7853981633974483
toDegrees:90.0
3.141592653589793


----------


Java xxxValue()



Input


class test{


public static void main(String[] args){

Integer x = 5;
System.out.println(x.byteValue());
System.out.println(x.doubleValue());
System.out.println(x.floatValue());
System.out.println(x.intValue());
System.out.println(x.longValue());
System.out.println(x.shortValue());

}
}

Output

D:\javafiles>java test.java
5
5.0
5.0
5
5
5

Reference: http://www.runoob.com/java/number-xxxvalue.html----------


Java compareTo()

Input


public class test{
public static void main(String args[]){

Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
}
}


Output

D:\javafiles>java test.java
false
true
false

Reference: http://www.runoob.com/java/number-compareto.html
----------

Java equals()


Input


public class Test{
public static void main(String args[]){
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;

System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
}
}

Output

D:\javafiles>java test.java
false
true
false

Reference: http://www.runoob.com/java/number-equals.html
----------

Java valueOf()

Input

public class test{
public static void main(String[] args){

Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444",16); //16 means use Hexadecimal
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}

}

Output

D:\javafiles>java test.java
9
5.0
80.0

1092

Reference: http://www.runoob.com/java/number-valueof.html
----------

Java toString()


Input

public class test{
public static void main(String[] args){

Integer x = 5;

System.out.println(x.toString());
System.out.println(Integer.toString(12));
}

}

Output

D:\javafiles>java test.java
5
12

Reference: http://www.runoob.com/java/number-tostring.html
----------

Java parseInt()


Input
public class test{
public static void main(String[] args){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);

System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}

Output
D:\javafiles>java test.java
9
5.0
1092

Reference: http://www.runoob.com/java/number-parseInt.html
---------

Java abs()

Input 
public class test{
public static void main(String[] args){
Integer a = -8;
double d = -100;
float f = -90;
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}

}

Output

D:\javafiles>java test.java
8
100.0
90.0

Reference: http://www.runoob.com/java/number-abs.html
---------

Java ceil() & floor()


Input 

public class test{
public static void main(String[] args){
double d = 100.675;
float f = -90;
System.out.println(Math.ceil(d));
System.out.println(Math.ceil(f));
System.out.println(Math.floor(d));
System.out.println(Math.floor(f));
}

}
Output

D:\javafiles>java test.java
101.0
-90.0
100.0
-90.0

Reference: http://www.runoob.com/java/number-ceil.html
---------

Java rint()

Input 
public class test{
public static void main(String[] args){
double d = 100.675;
double e = 100.500;
double f = 100.200;
System.out.println(Math.rint(d));
System.out.println(Math.rint(e));
System.out.println(Math.rint(f));
}

}


Output
D:\javafiles>java test.java
101.0
100.0

100.0

Reference: http://www.runoob.com/java/number-rint.html
---------

Java round()

Input 
public class test{
public static void main(String[] args){
double d = 100.675;
double e = 100.500;
float f = 100;
float g = 90f;
System.out.println(Math.round(d));
System.out.println(Math.round(e));
System.out.println(Math.round(f));
System.out.println(Math.round(g));
}

}

Output
D:\javafiles>java test.java
101
101
100
90


Reference: http://www.runoob.com/java/number-round.html
---------

Java min()


Input 
public class test{
public static void main(String[] args){
System.out.println(Math.min(12.123, 12.456));
System.out.println(Math.min(23.12, 23.0));
}

}

Output

D:\javafiles>java test.java
12.123
23.0

Reference: http://www.runoob.com/java/number-min.html
---------

Java max()

Input 

public class test{
public static void main(String[] args){
System.out.println(Math.max(12.123, 18.456));
System.out.println(Math.max(23.12, 23.0));
}

}
Output

D:\javafiles>java test.java
18.456
23.12

Reference: http://www.runoob.com/java/number-max.html
---------

Java exp()


Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of exp(%.3f) : %.3f%n", x, Math.exp(x));
}

}

Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of exp(11.635) : 112983.831

Reference: http://www.runoob.com/java/number-exp.html
---------

Java log()


Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of log(%.3f) : %.3f%n", x, Math.log(x));
}

}

Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of log(11.635) : 2.454

Reference: http://www.runoob.com/java/number-log.html

---------

Java pow()

Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of pow(%.3f, %.3f): %.3f%n", x, y, Math.pow(x, y));
}

}

Output

D:\javafiles>java test.java
Value of e : 2.7183
Value of pow(11.635, 2.760): 874.008

Reference:http://www.runoob.com/java/number-pow.html
---------

Java sqrt()

Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e: %.4f%n", Math.E);
System.out.printf("Value of sqrt(%.3f): %.3f%n", x, Math.sqrt(x));
}

}

Output
D:\javafiles>java test.java
Value of e: 2.7183
Value of sqrt(11.635): 3.411

Reference: http://www.runoob.com/java/number-sqrt.html
---------

Java random()

Input 
public class test{
public static void main(String[] args){
System.out.println( Math.random() );
System.out.println( Math.random() );
}

}

Output
D:\javafiles>java test.java
0.7514961840542885
0.36280573195436616

Reference: http://www.runoob.com/java/number-random.html
---------

No comments:

Post a Comment