Java Number Math 类方法
生活随笔
收集整理的這篇文章主要介紹了
Java Number Math 类方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java Math 類(lèi)
Java 的 Math 包含了用于執(zhí)行基本數(shù)學(xué)運(yùn)算的屬性和方法,如初等指數(shù)、對(duì)數(shù)、平方根和三角函數(shù)。
Math 的方法都被定義為 static 形式,通過(guò) Math 類(lèi)可以在主函數(shù)中直接調(diào)用。
public class Test { public static void main (String []args) { System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2)); System.out.println("0度的余弦值:" + Math.cos(0)); System.out.println("60度的正切值:" + Math.tan(Math.PI/3)); System.out.println("1的反正切值: " + Math.atan(1)); System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2)); System.out.println(Math.PI); } }以上實(shí)例編譯運(yùn)行結(jié)果如下:
90 度的正弦值:1.0 0度的余弦值:1.0 60度的正切值:1.7320508075688767 1的反正切值: 0.7853981633974483 π/2的角度值:90.0 3.141592653589793floor,round 和 ceil 實(shí)例:
public class Main { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println("Math.floor(" + num + ")=" + Math.floor(num)); System.out.println("Math.round(" + num + ")=" + Math.round(num)); System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num)); } }以上實(shí)例執(zhí)行輸出結(jié)果為:
Math.floor(1.4)=1.0 Math.round(1.4)=1 Math.ceil(1.4)=2.0 Math.floor(1.5)=1.0 Math.round(1.5)=2 Math.ceil(1.5)=2.0 Math.floor(1.6)=1.0 Math.round(1.6)=2 Math.ceil(1.6)=2.0 Math.floor(-1.4)=-2.0 Math.round(-1.4)=-1 Math.ceil(-1.4)=-1.0 Math.floor(-1.5)=-2.0 Math.round(-1.5)=-1 Math.ceil(-1.5)=-1.0 Math.floor(-1.6)=-2.0 Math.round(-1.6)=-2 Math.ceil(-1.6)=-1.0總結(jié)
以上是生活随笔為你收集整理的Java Number Math 类方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 减肥一个月瘦了30斤,浑身无力,手脚凉
- 下一篇: Java substring() 方法