将双精度数四舍五入到最接近的(最小)值。

huangapple go评论70阅读模式
英文:

Rounding off double to closest (minimum) value

问题

我有一个方法,有时会返回0.28 / 0.23。
我正在使用下面的类

DecimalFormat df2 = new DecimalFormat("#.#");

但是这里我希望这段代码每次都返回0.2,但当它大于0.25时,会将其四舍五入为最接近的小数0.3。

但我希望我的代码在值在0.21到0.29之间变化时每次都返回0.2。有人可以帮助我吗?

英文:

I have a method which sometimes returns 0.28 / 0.23 .
I am using the below class

DecimalFormat df2 = new DecimalFormat("#.#");

but here I need this code to return me 0.2 every-time , but when its more than 0.25 its returns 0.3 by rounding it off to closet decimal .

But i want my code to return 0.2 every-time the value varies from 0.21 to 0.29.
can someone help me with this ?

答案1

得分: 4

你可以将 RoundingMode 设置为向下舍入。

df2.setRoundingMode(RoundingMode.DOWN);
英文:

You can set the RoundingMode to round down.

df2.setRoundingMode(RoundingMode.DOWN);

答案2

得分: 0

以下是翻译好的部分:

double[] data = {
        .21, .22, .22, .23, .24, .25, .26, .27, .28, .29
};
for (double value : data) {
    System.out.println(value + "  ->  " + ((int) (value * 10)) / 10d);
} 

输出结果:

0.21  ->  0.2
0.22  ->  0.2
0.22  ->  0.2
0.23  ->  0.2
0.24  ->  0.2
0.25  ->  0.2
0.26  ->  0.2
0.27  ->  0.2
0.28  ->  0.2
0.29  ->  0.2
英文:

You can also do it as follows without using DecimalFormat. To round down to the nearest 10th, just multiple by 10, casting as an int and then divide by 10d.

double[] data = {
        .21,.22,.22,.23,.24,.25,.26,.27,.28,.29
};
for (double value : data) {
    System.out.println(value + "  ->  " + ((int) (value * 10)) / 10d);
} 

prints

0.21  ->  0.2
0.22  ->  0.2
0.22  ->  0.2
0.23  ->  0.2
0.24  ->  0.2
0.25  ->  0.2
0.26  ->  0.2
0.27  ->  0.2
0.28  ->  0.2
0.29  ->  0.2




</details>



# 答案3
**得分**: 0

以下是您要翻译的内容:

There are a few ways to accomplish this.

Casting a floating-point value to an integer will truncate the decimal.
So, you could utilize this by first multiplying by 10, truncating the value, and then re-dividing by 10.

```java
float valueA = 0.28f;
float valueB = 0.23f;
valueA = ((int) (valueA * 10)) / 10f;
valueB = ((int) (valueB * 10)) / 10f;

Output

valueA = 0.2
valueB = 0.2

Additionally, you can utilize the BigDecimal class.
The setScale method allows you to set the amount of digits after the decimal point, in addition to setting a RoundingMode.

There are numerous types of rounding modes, see the RoundingMode JavaDoc linked above, for a table of values.

I believe you are looking for FLOOR, or DOWN, for the truncation.

Here is an example using DOWN.

BigDecimal valueA = new BigDecimal(&quot;0.28&quot;);
BigDecimal valueB = new BigDecimal(&quot;0.23&quot;);
valueA = valueA.setScale(1, RoundingMode.DOWN);
valueB = valueB.setScale(1, RoundingMode.DOWN);

Output

valueA = 0.2
valueB = 0.2

On a final note, you could just parse the value as a String and substring the value for 1 digit after the decimal point.

float valueA = 0.28f;
float valueB = 0.23f;
String stringA = String.valueOf(valueA);
valueA = Float.parseFloat(stringA.substring(0, stringA.indexOf(&#39;.&#39;) + 2));
String stringB = String.valueOf(valueB);
valueB = Float.parseFloat(stringB.substring(0, stringB.indexOf(&#39;.&#39;) + 2));

Output

valueA = 0.2
valueB = 0.2
英文:

There are a few ways to accomplish this.

Casting a floating-point value to an integer will truncate the decimal.
So, you could utilize this by first multiplying by 10, truncating the value, and then re-dividing by 10.

float valueA = 0.28f;
float valueB = 0.23f;
valueA = ((int) (valueA * 10)) / 10f;
valueB = ((int) (valueB * 10)) / 10f;

Output

valueA = 0.2
valueB = 0.2

Additionally, you can utilize the BigDecimal class.
The setScale method allows you to set the amount of digits after the decimal point, in addition to setting a RoundingMode.

There are numerous types of rounding modes, see the RoundingMode JavaDoc linked above, for a table of values.

I believe you are looking for FLOOR, or DOWN, for the truncation.

Here is an example using DOWN.

BigDecimal valueA = new BigDecimal(&quot;0.28&quot;);
BigDecimal valueB = new BigDecimal(&quot;0.23&quot;);
valueA = valueA.setScale(1, RoundingMode.DOWN);
valueB = valueB.setScale(1, RoundingMode.DOWN);

Output

valueA = 0.2
valueB = 0.2

On a final note, you could just parse the value as a String and substring the value for 1 digit after the decimal point.

float valueA = 0.28f;
float valueB = 0.23f;
String stringA = String.valueOf(valueA);
valueA = Float.parseFloat(stringA.substring(0, stringA.indexOf(&#39;.&#39;) + 2));
String stringB = String.valueOf(valueB);
valueB = Float.parseFloat(stringB.substring(0, stringB.indexOf(&#39;.&#39;) + 2));

Output

valueA = 0.2
valueB = 0.2

huangapple
  • 本文由 发表于 2023年7月17日 21:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定