java 将数字转换为带有 2 个小数位的浮点数

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

Converting numbers to float with 2 decimal places

javadecimal

提问by javacup

float f = 0.00f;
System.out.println(f);      

gives the output:

给出输出:

0.00

I'd like to format a number represented by a percentage to 2 decimal places. But the result should be a floatand nota string.

我想将百分比表示的数字格式化为 2 个小数位。但结果应该是一个浮点数不是一个字符串。

e.g.

例如

10.001 needs to be converted to 10.00
0.0 needs to be converted to 0.00
78.8  needs to be converted to 78.80 

The values thus formatted will be assigned to a float.. how would one accomplish this?

这样格式化的值将被分配给一个浮点数..如何实现这一点?

回答by Matheus

private float parse(float val){       
     DecimalFormat twoDForm = new DecimalFormat("#.##");
     return Float.valueOf(twoDForm.format(val));
}

As long as you call it passing an valid float, your result will be a float. But you can't show the right most zero if its not a String.

只要你调用它传递一个有效的浮点数,你的结果就是一个浮点数。但是如果它不是一个字符串,你就不能显示最右边的零。

回答by Mike Sherrill 'Cat Recall'

In the general case, you can't do that. There's no guarantee that a particular decimal value can be represented by a float that has only two digits right of the decimal.

在一般情况下,你不能这样做。不能保证特定的十进制值可以用小数点右边只有两位数的浮点数表示。

A float is the wrong data type for this kind of precision. You need to use a decimal type or a scaled integer instead.

对于这种精度,浮点数是错误的数据类型。您需要改用十进制类型或缩放整数。

Assignment works the same way. If you assign the value 133.47 to a floating-point variable, your environment will assign the closest valid floating-point numberto the variable. The closest valid floating-point number will probablynot be 133.47.

分配的工作方式相同。如果您将值 133.47 分配给浮点变量,您的环境将为该变量分配最接近的有效浮点数。最接近的有效浮点数可能不是 133.47。

You can compile and execute this program in C.

你可以用 C 编译和执行这个程序。

#include <stdio.h>

int main (void) {
  float r;
  r = 133.47;
  printf("%.2f, %f\n", r, r);
  return 0;
}

It prints these values on my system

它在我的系统上打印这些值

$ ./a.out
133.47, 133.470001

Formatting to two decimal places changed the way 'r' looks, but it didn't change its value. Your system will do floating-point arithmetic based on the actual value, not the formatted value. (Unless you also change the data type.)

格式化为两位小数改变了 'r' 的外观,但它并没有改变它的值。您的系统将根据实际值而不是格式化值进行浮点运算。(除非您还更改了数据类型。)

回答by user207421

Floats don't have decimal places. They have binary places. It follows that the only fractions that can be represented exactly in a float to two decimal places are 0, 0.25, 0.5, 0.75. In all the other cases what you are asking is impossible.

浮点数没有小数位。他们有二进制的地方。因此,唯一可以在浮点数中精确表示到两位小数的分数是 0、0.25、0.5、0.75。在所有其他情况下,您所要求的都是不可能的。

回答by JAB

import java.text.DecimalFormat;

public class Padding {
    public static void main(String[] args) {
        float value = 10.001f;
        DecimalFormat decimal = new DecimalFormat("0.00");

        String formattedValue = decimal.format(value);
        System.out.println(formattedValue);
    }

}

Output : 10.00

输出:10.00