Math 以及 DecimalFormat 學(xué)習(xí)總結(jié)

一 Math 常用相關(guān)用法的學(xué)習(xí)總結(jié)
1.Math.log10(num) 10的多少次方等于num
例如 Math.log10(100)=2;
2 Math.floor(num) 向下取整
例如 Math.floor(8.5)=8;
Math.floor(-8.5)=-9;
3.Math.pow(10, u) 返回10的u次方
例如 Math.pow(10,3)=1000;
4.Math.max(x,y) 返回x 和y中的最大值
例 Math.max(2,3)=3;
5.Math.min(x,y) 返回 x 和y的最小值
例 Math.min(0,1)=0;

Math.abs(x) 返回x的絕對值
例 Math.abs(-10)=10; Math.abs(10)=10; Math.abs(0)=0;

Math.round(x) 相當(dāng)于加0.5后向下取值
例 Math.round(3.3)=3.0+0.5=3.8; 向下取值為3
Math.round(-10.5)=-10.5+0.5=-10; 向下取值為-10

Math.ceil(x) 向上取整

例 Math.ceil(6.5)=7;

Math.addExact(x,y) x y相加
Math.multiplyExact(x,y) x y相乘

Math.PI =圓周率 3.14159265358979323846

二DecimalFormat 學(xué)習(xí)總結(jié)
DecimalFormat 是 NumberFormat 的一個具體子類,用于格式化十進(jìn)制數(shù)字。
DecimalFormat 包含一個模式 和一組符號
符號含義:
0 一個數(shù)字
“# ” (在文章中不顯示 因此添加雙引號)
一個數(shù)字,不包括 0
. 小數(shù)的分隔符的占位符
, 分組分隔符的占位符
; 分隔格式。

  • 缺省負(fù)數(shù)前綴。
    % 乘以 100 和作為百分比顯示

例子:

DecimalFormat df1 = new DecimalFormat("0.0");

DecimalFormat df2 = new DecimalFormat("#.#");

DecimalFormat df3 = new DecimalFormat("000.000");

DecimalFormat df4 = new DecimalFormat("###.###");

System.out.println(df1.format(12.34));

System.out.println(df2.format(12.34));

System.out.println(df3.format(12.34));

System.out.println(df4.format(12.34));

結(jié)果:

12.3

12.3

012.340

12.34

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容