Java 快速導覽 - static round()

Math 類別有 static round() 方法 (method) ,回傳最接近參數 (parameter) 的整數值

方法描述
long round(double d)
int round(float f)
回傳最接近參數的整數值


舉例如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class RoundDemo {
    public static void main(String[] args) {
        System.out.println(Math.round(9.0));
        System.out.println(Math.round(9.1));
        System.out.println(Math.round(9.2));
        System.out.println(Math.round(9.3));
        System.out.println(Math.round(9.4));
        System.out.println(Math.round(9.5));
        System.out.println(Math.round(9.6));
        System.out.println(Math.round(9.7));
        System.out.println(Math.round(9.8));
        System.out.println(Math.round(9.9));
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:RoundDemo.java
    功能:示範物件導向的基本觀念
    作者:張凱慶
    時間:西元 2010 年 10 月 */


編譯後執行,結果如下



中英文術語對照
方法method
參數parameter






沒有留言: