Rounding up a number to nearest multiple of 5

后端 未结 21 1989
感动是毒
感动是毒 2020-11-27 15:24

Does anyone know how to round up a number to its nearest multiple of 5? I found an algorithm to round it to the nearest multiple of 10 but I can\'t find this one.

T

21条回答
  •  情歌与酒
    2020-11-27 16:20

    Another Method or logic to rounding up a number to nearest multiple of 5

    double num = 18.0;
        if (num % 5 == 0)
            System.out.println("No need to roundoff");
        else if (num % 5 < 2.5)
            num = num - num % 5;
        else
            num = num + (5 - num % 5);
        System.out.println("Rounding up to nearest 5------" + num);
    

    output :

    Rounding up to nearest 5------20.0
    

提交回复
热议问题