java 用 .00 获得双倍

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17277744/
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-11-01 01:34:12  来源:igfitidea点击:

Getting a double with .00

javadouble

提问by Sebastián Aguerre

I have a Stringvalue; and i need to have it always with 2 decimals as a Doubletype; so, if i get 500, i need to convert it to 500.00being always a double value.

我有一个String价值;而且我需要始终使用 2 位小数作为Double类型;所以,如果我得到500,我需要将它转换500.00为始终是双值。

My problem is when i have something likexxx.00or xxx.x0, the parseDouble method removes the second 0.

我的问题是当我有类似xxx.00或的东西时xxx.x0,parseDouble 方法会删除第二个 0。

My code is something like this

我的代码是这样的

String number = "500.00";
Double numberToSave = (Double.ParseDouble(number));

And numberToSavereturns 500.0

numberToSave返回500.0

Any ideas of how can i resolve this? I need to show the second decimal even if it is 0.

我该如何解决这个问题的任何想法?我需要显示第二个小数,即使它是0.

Thank you.

谢谢你。

回答by Jon Skeet

Doubledoesn't have any concept of maintaining insignificant digits.

Double没有任何保留无意义数字的概念。

The problem isn't the Double(or double) value you're using - it's how you then convert it backinto a string. You should use a DecimalFormatwhich specifies the appropriate number of decimal places.

问题不在于您使用的Double(or double) 值——而是您如何将其转换字符串。您应该使用 aDecimalFormat来指定适当的小数位数。

Also note that you're currently using Double.parseDoublewhich returns a double, but you're assigning it to a Doublevariable, which will box the value - potentially unnecessarily.

另请注意,您当前正在使用Double.parseDoublewhich 返回 a double,但您将其分配给一个Double变量,该变量会将值装箱 - 可能是不必要的。

Sample code:

示例代码:

import java.text.DecimalFormat;

public class Test {
    public static void main(String[] args) {
        String text = "500.00";
        double number = Double.parseDouble(text);

        DecimalFormat format = new DecimalFormat("0.00");
        String formatted = format.format(number);
        System.out.println(formatted);
    }
}

Note that the above code will give you two decimal digits even if there are moredigits available.

请注意,即使有更多可用数字,上述代码也会为您提供两位十进制数字。

Additionally, if exact decimal digits matter to you, you might want to consider using BigDecimalinstead of double. This is particularly important if your values actually represent currency. You should understand the difference between a floating binarypoint type (such as double) and a floating decimalpoint type (such as BigDecimal) and choose appropriately between them.

此外,如果精确的十进制数字对您很重要,您可能需要考虑使用BigDecimal代替double。如果您的值实际上代表货币,这一点尤其重要。您应该了解浮点二进制小数点类型(例如double)和浮点小数点类型(例如BigDecimal)之间的区别,并在它们之间进行适当的选择。

回答by arshajii

There's always String.formatas well,

也总String.format有,

String.format("%.2f", d)

If you're only worried about printing the formatted value, you can use System.out.printf,

如果您只担心打印格式化的值,则可以使用System.out.printf,

double d = 500;
System.out.printf("%.2f%n", d);  // %n is a line-break
500.00

回答by FrustratedWithFormsDesigner

You might want to look into the DecimalFormatclass, it can be used to format a doubles like this:

您可能想查看DecimalFormat该类,它可用于格式化双打,如下所示:

import java.text.*;

public class FormatDoubleTest
{
    public static void main(String[] args)
    {
        double d1 = 500.00;
        double d2 = 500.001;
        double d3 = 500.009;

        DecimalFormat df = new DecimalFormat("0.00");

        System.out.println(df.format(d1));
        System.out.println(df.format(d2));
        System.out.println(df.format(d3));

    }
}

回答by JHS

You need to use the DecimalFormatclass.

您需要使用DecimalFormat该类。

Something like this DecimalFormat df = new DecimalFormat("#.##");.

像这样的东西DecimalFormat df = new DecimalFormat("#.##");

Have a look at the java docs.

看看java 文档

回答by Pranav Hosangadi

Use a NumberFormatobject.

使用一个NumberFormat对象。

String number = "500.00";
Double numberToSave = (Double.ParseDouble(number));
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(3);
System.out.println(nf.format(numberToSave));