java 在小数点后添加 n 个零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11895863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:48:53  来源:igfitidea点击:

adding n number of zeros after decimal

javaandroid

提问by Android

I need to give accuracy of a number entered by user till few digit. like if user enter some random value and gives that he wants accuracy till three digit then I need to round off the digit till three places after decimal. So i did something like this

我需要给出用户输入的数字的准确性,直到几位数。就像如果用户输入一些随机值并给出他想要精确到三位数那么我需要将数字四舍五入到小数点后三位。所以我做了这样的事情

 int index = value.indexOf('.'); 
            if (index >= 0)
            {
                String fractional = value.substring(index);

                if (fractional.length() > decimalPlaces)
                {
                    floatValue = roundOffDecimals(floatValue, decimalPlaces);
                }
            }
            retVal = new Float(floatValue);

but when user enters some value but do not enters any value as decimal I need to display it as dismal value with zeros as number of decimal places. like for 15 is his number and 3 is his accuracy then I need to display number as 15.000

但是当用户输入一些值但不输入任何值作为小数时,我需要将它显示为带有零作为小数位数的惨淡值。比如 15 是他的数字,3 是他的准确度,那么我需要将数字显示为 15.000

I am not able to display zero after decimal when it always changes. Please help. I tried DeciamlFormat df = new DecimalFormat("",#.##); but takes static value. And my accuracy keeps changing.

当它总是改变时,我无法在十进制后显示零。请帮忙。我试过 DeciamlFormat df = new DecimalFormat("",#.##); 但采用静态值。而且我的准确度一直在变化。

Any help will be appreciated.

任何帮助将不胜感激。

回答by Bijoy Thangaraj

You can indeed use NumberFormat to do this

你确实可以使用 NumberFormat 来做到这一点

double amount = 15;
NumberFormat formatter = new DecimalFormat("#0.000");
System.out.println("The Decimal Value is:"+formatter.format(amount));

回答by Luiggi Mendoza

You can create a method that returns the exact format for your decimals. Here is an example:

您可以创建一个方法来返回小数的确切格式。下面是一个例子:

public String formatNumber(int decimals, double number) {
    StringBuilder sb = new StringBuilder(decimals + 2);
    sb.append("#.");
    for(int i = 0; i < decimals; i++) {
        sb.append("0");
    }
    return new DecimalFormat(sb.toString()).format(number);
}

If you don't need to change the decimalsvalue so often, then you can change your method to something like:

如果您不需要decimals经常更改值,那么您可以将方法更改为:

public DecimalFormat getDecimalFormat(int decimals) {
    StringBuilder sb = new StringBuilder(decimals + 2);
    sb.append("#.");
    for(int i = 0; i < decimals; i++) {
        sb.append("0");
    }
    return new DecimalFormat(sb.toString());
}

回答by user12817487

you can use string format:

您可以使用字符串格式:

Double area = 6

双面积 = 6

String ar = String.format("%.3f", area); ar = 6.000

String ar = String.format("%.3f", area); ar = 6.000