如何将具有两位小数且小数部分为0的数字添加到数组中。

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

How to add 2 decimal places having 0s to an array

问题

我想将数组中所有元素的精度设置为2。

例如,如果我输入11.00,我期望输出为:

[11.00, 11.04, 11.09, 11.15, 11.19, 11.22]

但实际输出为:

[11.0, 11.04, 11.09, 11.15, 11.19, 11.22]

其中第一个元素缺少尾随的零。

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

import static java.lang.Math.*;
public class Main{
  public static void main (String[] args){
    Scanner s = new Scanner(System.in);
    DecimalFormat df = new DecimalFormat(".00");
    df.setMaximumFractionDigits(2);
    float a = s.nextFloat();
    float arr[] = new float[6];
    arr[0] = a;
    arr[1] = a + (float)(0.04);
    arr[2] = a + (float)(0.09);
    arr[3] = a + (float)(0.15);
    arr[4] = a + (float)(0.19);
    arr[5] = a + (float)(0.22);
    System.out.println(Arrays.toString(arr));
  }
}
英文:

I want to print precision of 2 for all elements in my array.

For example, if I input 11.00, I expect output:

[11.00, 11.04, 11.09, 11.15, 11.19, 11.22]

but I get:

[11.0, 11.04, 11.09, 11.15, 11.19, 11.22]

Where the first element is missing trailing zero.

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

import static java.lang.Math.*;
public class Main{
  public static void main (String[] args){
	Scanner s = new Scanner(System.in);
	DecimalFormat df = new DecimalFormat(".00");
    df.setMaximumFractionDigits(2);
	float a=s.nextFloat();
	float arr[]= new float[6];
	// arr[0]=Float.valueOf((df.format(a+0.001)));
	arr[0]=a;
	arr[1]=a+(float)(0.04);
	arr[2]=a+(float)(0.09);
	arr[3]=a+(float)(0.15);
	arr[4]=a+(float)(0.19);
	arr[5]=a+(float)(0.22);
	System.out.println(Arrays.toString(arr));
	
  }
}

答案1

得分: 1

你的数组中的对象类型是 float。你无法通过 N 个小数位来指定它为一个浮点数,它只是一个基本值。

通常情况下,当你需要使用它时,你希望格式化这个基本值,例如作为一个 String

在这种情况下:

for (float f : arr) {
    System.out.println(df.format(f));
}
英文:

The object type in your array is a float. You cannot tell it to be a float with N decimal places, it is just a primitive value.

Typically you want to format the primitive when you need to use it, e.g. as a String.

In this case:

for( float f : arr )
{
	System.out.println( df.format( f ) );
}

答案2

得分: 1

让我们从为什么不起作用开始。从Arrays.toString文档中:

> 元素被转换为字符串,如同<tt>String.valueOf(float)</tt>。

String.valueOf(float)的实现如下:

public static String valueOf(float f) {
    return Float.toString(f);
}

因此在您的代码片段中声明的 df 从未被使用。

你可以怎么做呢?如@Koenigsberg在他的回答中提到的,你可以遍历数组,然后在两侧添加括号,在元素之间添加逗号。

如果你可以使用双精度浮点数而不是单精度浮点数,你可以使用Java流。代码示例如下:

System.out.println("[" + Arrays.stream(arr).mapToObj(df::format).collect(Collectors.joining(", ")) + "]");

如果不能使用双精度浮点数,你可以遍历arr的索引:

System.out.println("[" + IntStream.range(0, arr.length).mapToObj(i -> df.format(arr[i])).collect(Collectors.joining(", ")) + "]");
英文:

Let's start with why doesn't it work? From Arrays.toString docs:

> Elements are converted to strings as by <tt>String.valueOf(float)</tt>.

The implementation of String.valueOf(float) is:

public static String valueOf(float f) {
    return Float.toString(f);
}

Therefore the df declared in your code snippet is never used.

What can you do? As @Koenigsberg mentioned in his answer, you can iterate over the array, and then add brackets on the sides, and commas between the elements.

If you can use double instead of floats, you can use java streams. Code sample will be:

System.out.println(&quot;[&quot; + Arrays.stream(arr).mapToObj(df::format).collect(Collectors.joining(&quot;, &quot;)) + &quot;]&quot;);

If you can't use doubles, you can just iterate over the indices of arr:

System.out.println(&quot;[&quot; + IntStream.range(0, arr.length).mapToObj(i -&gt; df.format(arr[i])).collect(Collectors.joining( &quot;, &quot;)) + &quot;]&quot;);

huangapple
  • 本文由 发表于 2020年10月26日 17:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64534092.html
匿名

发表评论

匿名网友

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

确定