Java Tutorial
IntroductionEnvironment SetupIDEBuild ManagementLanguage SpecificationBasic ProgramVariablesData TypesPackagesModifiersConditionalsLoopsObject OrientedClassesSuperInterfacesEnumStatic importInheritanceAbstractionEncapsulationPolymorphismBoxing & UnboxingConversion Formatting numbers Arrays Command line arguments in java Variable Number of arguments in Java Exception handling in Java String handling in Java StringBuffer and StringBuilder in Java Mathematical Operations in Java Date and Time in Java Regular expressions in Java Input output programming in Java File Handling Nested Classes Collections Generics Serialization Socket programming Multi-Threading Annotations Lambda Expressions Reflections in Java Singleton class in Java Runtime Class in JavaHow to load resource in JavaHow to load properties file in JavaAdvanced
Log4j – Logging framework in JavaInterview Questions in JavaIn this post, we will learn how to perform mathematical operations on numbers – power, squre root, floor, ceil, rounding numbers Here is the program that illustrates various mathematical operations in Java.
package numbers;
/**
* Created by ssalunke on 14/04/2016.
*/
public class Maths {
public static void main(String [] args) throws Exception {
//Get absolute value
System.out.println("Absolute value of -2929.2 -> " + Math.abs(-2929.2));
//Rounding to 0 decimals
System.out.println("Round of the number -234.221 is ->" +
" " + Math.round(-234.221));
//Power method
System.out.println("2^3 is -> " + Math.pow(2, 3));
//Finding square root
System.out.println("Square root of 525 -> " + Math.sqrt(525));
//Get random value
System.out.println("Random number -> " + String.valueOf(Math.random()));
}
}
Here is the output of above example.
Absolute value of -2929.2 -> 2929.2
Round of the number -234.221 is -> -234
2^3 is -> 8.0
Square root of 525 -> 22.9128784747792
Random number -> 0.6005728946831648
Web development and Automation testing
solutions delivered!!