Tool class: Math

Source: Internet
Author: User

Java.lang.Math: One of the most important Java tools classes, providing many common mathematical methods.

code example :

 Public classTest { Public Static voidMain (string[] args) {DoubleA = 10.5; Doubleb =-10.5; //ABS: Take absolute valueSystem.out.println (A + "absolute value:" +Math.Abs (a)); System.out.println (b+ "Absolute value:" +Math.Abs (b)); System.out.println ("--------------------"); //ceil: Takes the smallest integer larger than the passed-in parameter (double type)System.out.println (A + "larger minimum integer:" +Math.ceil (a)); System.out.println (b+ "Larger minimum integer:" +Math.ceil (b)); System.out.println ("--------------------"); //Floor : Takes the largest integer (double type) smaller than the incoming parameterSystem.out.println (A + "smaller maximum integer:" +Math.floor (a)); System.out.println (b+ "Smaller maximum integer:" +Math.floor (b)); System.out.println ("--------------------"); //random number between random:0-1 (double type)System.out.println ("Random number between 0-1:" +math.random ()); System.out.println ("Random number between 0-1:" +math.random ()); System.out.println ("--------------------"); //rint: Rounding (even if *.5)System.out.println ("Rint Rounding:" +Math.rint (a)); System.out.println ("Rint Rounding:" +Math.rint (b)); System.out.println ("Rint Rounding:" +math.rint (10.1)); System.out.println ("Rint Rounding:" +math.rint (10.7)); System.out.println ("--------------------"); //Round: Rounding (float returns int,double return long)System.out.println ("Round rounding:" +Math.Round (a)); System.out.println ("Round rounding:" +Math.Round (b)); System.out.println ("Round Rounding:" +math.round (10.1)); System.out.println ("Round Rounding:" +math.round (10.7)); System.out.println ("--------------------"); //Max: Take a larger number min: Go to a smaller numberSystem.out.println ("Larger number:" +Math.max (A, b)); System.out.println ("Smaller number:" +Math.min (A, b)); System.out.println ("--------------------"); }} Output:Absolute value of 10.5: The absolute value of 10.5-10.5:10.5--------------------10.5 The largest smallest integer: 11.0-10.5 The larger smallest integer: -10.0--------------------10.5 Smaller maximum integer: 10.0-10 .5 Small Maximum integer: -11.0--------------------0-1 random number: 0.33381578000626720-1:0.3831866162450892------------------- -rint Rounding:10.0rint Rounding:-10.0rint Rounding:10.0rint Rounding:11.0--------------------Round rounding:11Round rounding:-10Round rounding:10Round rounding:--------------------larger number:10.5smaller number:-10.5--------------------

Tool class: Math

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.