java 少数以下の切り上げを行う

java 少数以下の切り上げを行う

javaで、少数以下の切り上げを行う手順を記述してます。「Math.ceil​」に対象の数値を指定します。

環境

  • OS windows11 home
  • java 19.0.1

手順

少数以下の切り上げを行うには、「Math.ceil​」で可能です。

Math.ceil(数値)

実際に、使用してみます。

public class App {
    public static void main(String[] args) throws Exception {

        System.out.println(Math.ceil(1.11)); // 2.0
        System.out.println(Math.ceil(1.99)); // 2.0
        System.out.println(Math.ceil(-2.99)); // -2.0
        System.out.println(Math.ceil(-2.11)); // -2.0

    }
}

切り上げされていることが確認できます。

桁数指定

桁数指定する場合は、以下のように一度演算してから「Math.ceil」を実行します。

public class App {
    public static void main(String[] args) throws Exception {

        System.out.println(Math.ceil(1.11 * 10)/10); // 1.2
        System.out.println(Math.ceil(1.111 * 100)/100); // 1.12

    }
}