Round off a double while maintaining the trailing zero

血红的双手。 提交于 2021-02-08 12:54:07

问题


Here is my function to roundoff a number upto two decimals but when the rounded off number is 1.50 it seems to ignore the trailing zero and just returns 1.5

public static double roundOff(double number) {
        double accuracy = 20;
        number = number * accuracy;
        number = Math.ceil(number);
        number = number / accuracy;
        return number;
    }

So if I send 1.499 it returns 1.5 where as I want 1.50


回答1:


This is a printing poblem:

double d = 1.5;
System.out.println(String.format("%.2f", d)); // 1.50



回答2:


1.5 is, number of significant digits notwithstanding, the same as 1.50 (and even 1.5000000000000).

You need to separate the value of the number from its presentation.

If you want it output with two decimal digits, just use String.format, such as with:

public class Test
{
    public static void main(String[] args) {
        double d = 1.50000;
        System.out.println(d);
        System.out.println(String.format("%.2f", d));
    }
}

which outputs:

1.5
1.50

If you still want a function that does all that for you and gives you a specific format, you'll need to return the string with something like:

public static String roundOff(double num, double acc, String fmt) {
    num *= acc;
    num = Math.ceil(num);
    num /= acc;
    return String.format(fmt, num);
}

and call it with:

resultString = roundOff(value, 20, "%.2f"); // or 100, see below.

This will allow you to tailor the accuracy and output format in any way you desire, although you can still hard-code the values if you want simplicity:

public static String roundOff(double num) {
    double acc = 20;
    String fmt = "%.2f";
    num *= acc;
    num = Math.ceil(num);
    num /= acc;
    return String.format(fmt, num);
}

One final note: your question states that you want to round to "two decimals" but that doesn't quite gel with your use of 20 as the accuracy, since that will round it up to the next multiple of 1/20. If you really want it rounded up to two decimals, the value you should be using for accuracy is 100.




回答3:


You will have to format it as a String in order to do that. Java, like most languages, will drop the trailing zero.

String.format("%.2f", number);

So you can either return a String (change your return type from double) or just format it when you need to display it using the code above. You can read the JavaDoc for Formatter in order to understand all of the possibilities with number of decimal places, comma placement, etc.




回答4:


You can try this if you want is a String output

double number = roundOff(1.499);//1.5

DecimalFormat decimalFormat = new DecimalFormat("#.00");
String fromattedDouble = decimalFormat.format(number);//1.50

The function roundOff is the same as you mentioned in your question.



来源:https://stackoverflow.com/questions/27832131/round-off-a-double-while-maintaining-the-trailing-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!