Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java Mathematical tool Class Math

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use java mathematical tool class Math". In daily operation, I believe that many people have doubts about how to use java mathematical tool class Math. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use java mathematical tool class Math". Next, please follow the editor to study!

1. Overview

Java.util.Math class is a mathematics-related tool class, which provides a large number of static methods to complete the operations related to mathematical operations.

two。 The basic method

Public static double abs (double num); gets the absolute value. There are many overloads, absolutely absolutely public static double ceil (double num); rounding up, ceil means ceiling public static double floor (double num); rounding down, floor means floor public static long round (double num); rounding is rounded into five pairs (see the comments on the code below), round has about, complete meaning.

3. The four methods are demonstrated by code together.

Public class MathMethod {public static void main (String [] args) {/ / abs method, take the absolute value System.out.println (Math.abs (3.14)); / / 3.14 System.out.println (Math.abs (0)); / / 0 System.out.println (Math.abs (- 2.2)); / / 2.2 System.out.println ("-") / / ceil method, rounding up to System.out.println (Math.ceil (3.2)); / / 4.0System.out.println (Math.ceil (3.8)); / / 4.0System.out.println (Math.ceil (- 3.2)); / /-3.0System.out.println (Math.ceil (- 3.8)) / /-3.0 System.out.println ("-"); / / floor method, rounding down to System.out.println (Math.floor (3.2)); / / 3.0 System.out.println (Math.floor (3.8)); / / 3.0 System.out.println (Math.floor (- 3.2)) / /-4.0System.out.println (Math.floor (- 3.8)); / /-4.0System.out.println ("-")

/ / [Note, interview high frequency] round method, rounding 50 pairs / / look at rounding first. If there is a negative number, first convert it to a positive number, then round it up, and then add the minus sign System.out.println (Math.round (3.4)); / / 3 System.out.println (Math.round (3.6)); / / 4System.out.println (Math.round (- 3.4)) / /-3 System.out.println (Math.round (- 3. 6)); / /-4 / / what do you mean by 50 pairs? When there is a 0.5 ending, add + 0.5 floor to it / / then perform the floor operation System.out.println (Math.round (- 2.5)) on the added result; / /-2 System.out.println (Math.floor (- 2.5 + 0.5)); / / consistent with the Math.round (- 2.5) result (Math.round (2.5)) / / 3 System.out.println (Math.floor (2.5 + 0.5)); / / consistent with Math.round (2.5)}}

4. Pi Math.PI

In the source code of the Math class, we can see that its custom pi PI = 3.14159265358979323846

If future calculations need to use PI, try to use the defined pi, which is very accurate.

At this point, the study on "how to use java mathematical tools Math" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report