如何显示星号的数量到最接近的千?

时间:2015-02-20 06:19:37

标签: java rounding

    bar1 = store1/1000;
    System.out.print("Store 1: ");
    for(int i = 1; i <= bar1; i++)
        System.out.print("*");

如何显示“*”的数字向上或向下舍入到最接近的千位数。现在它只是向下舍入。

2 个答案:

答案 0 :(得分:1)

使用

bar1 = (store1+500)/1000;
    System.out.print("Store 1: ");
    for(int i = 1; i <= bar1; i++)
        System.out.print("*");

答案 1 :(得分:1)

使用此代码:

    double store1 = 1095;
    long bar1 = Math.round(store1 / 1000);
    // int bar1 = store1/1000;
    System.out.print("Store 1: ");
    for (int i = 1; i <= bar1; i++)
        System.out.print("*");

或者如果stre 1是int并且您无法更改它,则可以使用:

    int store1 = 1095;
    long bar1 = Math.round(((double)store1) / 1000);
    // int bar1 = store1/1000;

它会将store1 / 1000的值四舍五入到最近的1000。

相关问题