Java Math class Knowledge points Summary

Source: Internet
Author: User
Tags square root string format

(1) When writing a program, you may need to calculate the square root of a number, the absolute value, get a random number, and so on. The math class in the Java.lang package contains a number of class methods that are used for scientific computation, which can be called directly from the class name. In addition, the math class has two static constants, E and pi, and their values are 2.7182828284590452354 and 3.14159265358979323846, respectively.

The following are common methods of the math class:

public static long ABS (double A) returns the absolute value of a

public static double Max (double a,double b) returns the maximum value of a, b

public static double min (double a,double b) returns the minimum value of a, b

public static double random () produces a random number between 0 and 1 (does not contain 0 and 1)

public static Double pow (double a,double b) returns a power of B

public static double sqrt (double A) returns the square root of a

public static double log (double a) returns the logarithm of a

public static double sin (double A) returns the sine value

public static double Asin (double A) returns the inverse chord value

(2) Sometimes it may be necessary to format the output of the digital results, For example, 3.14356789, you want to keep the decimal place 3 bits, the integer part to display at least 3 bits, the 3.14356789 format is 003.144, you can use the NumberFormat class in the Java.text package, which calls the class method:

public static final NumberFormat getinstance () instantiates a NumberFormat object that calls public final String format (double number) method to format a numeric number.

The NumberFormat class has the following common methods:

public void setmaximumfractiondigits (int newvalue)

public void setminimumfractiondigits (int newvalue)

public void setmaximumintegerdigits (int newvalue)

public void setminimumintegerdigits (int newvalue)

(3)

Java Math class Knowledge points Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.