简体   繁体   English

Java DecimalFormat.format不格式化数字?

[英]Java DecimalFormat.format does not format the number?

I have the method below, but for some reason it is not formatting the area to have only one decimal. 我有下面的方法,但由于某种原因,它没有格式化区域只有一个小数。 All help is appreciated. 所有帮助表示赞赏。

public double beraknaArea()
    {
       DecimalFormat formatter = new DecimalFormat("#0.0");
       double area = 0;
       area = radie*radie*3.14;
       formatter.format(area);
       return area;
     }

You are returning the same double, and not the output of the formatter. 您将返回相同的double,而不是formatter的输出。

The format() method does not alter the double that you pass in, it returns a String format of the double. format()方法不会改变传入的double,它返回double的String格式。

No methods in Java may change the type of a declared variable, or mutate any primitive value (or indeed String ). Java中的任何方法都不能更改声明的变量的类型,也不会改变任何原始值(或者确实是String )。 You need to return what formatter.format(area) returns, not the unchanged value of area . 您需要返回formatter.format(area)返回的内容,而不是未更改的area值。

public String beraknaArea()
{
   DecimalFormat formatter = new DecimalFormat("#0.0");
   double area = radie*radie*3.14;
   return formatter.format(area);
 }

Formatters format method return String Formatters 格式方法返回String

String temp = formatter.format(area);

System.out.println(temp);

Now you will see formatted code. 现在您将看到格式化的代码。

your statement formatter.format(area); 你的陈述formatter.format(area); is not modifying the variable area , the variable is still holding the same value as before, you may return a string if you want fromated output 没有修改变量area ,变量仍然保持与以前相同的值,如果你想要输出变量,你可以返回一个字符串

The problem is that instead of returning the formatted area, you are returning the area which you computed, but which is not formatted. 问题是,您没有返回格式化区域,而是返回您计算的区域,但未格式化。

Refer following: 参考如下:

public String beraknaArea()
    {
       DecimalFormat formatter = new DecimalFormat("#0.0");
       double area = 0;
       area = radie*radie*3.14;
       String formattedArea = formatter.format(area);
       return formattedArea;
     }



/************ OR **************/



public double beraknaArea()
    {
       DecimalFormat formatter = new DecimalFormat("#0.0");
       double area = 0;
       area = radie*radie*3.14;
       String formattedArea = formatter.format(area);
       return Double.parseDouble(formattedArea);
     }

The first solution returns the formattedArea as a String, whereas second one returns double value of formattedArea. 第一个解决方案将formattedArea作为String返回,而第二个解决方案返回formattedArea的double值。

You could multiply the number by 10, cast it as an integer, and then divide it by 10.0 to get a double with exactly one decimal point. 您可以将数字乘以10,将其转换为整数,然后除以10.0得到一个只有一个小数点的双精度数。

area = ((int) (area * 10)) / 10.0;

Instead of casting to an int, you could use Math.floor 您可以使用Math.floor而不是转换为int

area = Math.floor(area * 10) / 10.0;

Now, further arithmetic could make the number have a different number of decimal places. 现在,进一步的算术可以使数字具有不同的小数位数。

Edit: 编辑:

This is assuming the OP wants to keep the return value as a double instead of a String (as other answers have provided). 这假设OP希望将返回值保持为double而不是String(正如其他答案所提供的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM