简体   繁体   中英

Java: How to round up all of the elements in an array to two digits after decimal point?

I have a problem with rounding double values from an array.

When I started learning JAVA, I was surprised that the printing of arrays is different. What I mean is, in order to print out the whole array full of float values, you can't just loop (i) times and do System.out.println("%.2f", array[i]); .

The way to print it is System.out.println(Arrays.toString(array));

If it wasn't an array, I would round up the float just with System.out.println("%.2f, val) . However, with an array, I don't know how to do it. Appreciate the help.

Here is my whole class for calculating. Every calculation is correct and well, the only problem is rounding and printing.

package sample;

import java.util.Arrays;

public class Calculations extends Data{

    public void calculateLinearCredit()
    {
        double[] linearCredit = new double[super.term];
        double[] linearInterest = new double [super.term];
        double[] linearPayment = new double [super.term];


        double firstMonthInterest = ((super.loan / super.term) * super.percentage) / 100;
        double Credit = super.loan / super.term;
        double interestDiff = firstMonthInterest/12;

        linearInterest[0] = firstMonthInterest;


        for(int i = 0; i < linearCredit.length; i++)
        {
            linearCredit[i] = Credit;
        }

        for(int i = 1; i < linearInterest.length; i++)
        {
            linearInterest[i] = linearInterest[i-1] - interestDiff;
        }


        for(int i = 0; i < linearPayment.length; i++)
        {
            linearPayment[i] = linearInterest[i] + linearCredit[i];
        }

        //-----------------------------------------------
        //System.out.println(Arrays.toString(linearPayment));
        //System.out.println(Arrays.toString(linearCredit));
        //System.out.println(Arrays.toString(linearInterest));
    }
}

you can't just loop (i) times and do System.out.println("%.2f", array[i])

Of course you can. Just use the right method.

float[] array = new float[]{1.2f, 2.3425f, 3.4525f};
for (int i = 0; i < array.length; i++) {
  System.out.printf("%.2f%n", array[i]);
}

You can even do:

for (float value : array) {
  System.out.printf("%.2f%n", value);
}

If you fancy the new Stream API you could also do this (you have to use double though for some intricate type system reasons I shouldn't go into here).

double[] dblArray = new double[]{1.2, 2.3425, 3.4525};
Arrays.stream(dblArray)
        .forEach(value -> System.out.printf("%.2f%n", value));

Simply use Math.round()

double[] array = new double[]{12.5698, 123.12345,0.1, 41234.212431324};    
    for (int i=0;i<array.length;i++) {
     array[i] = Math.round(array[i] * 100.0) / 100.0;
    System.out.println(array[i]);
}

in order to print out the whole array full of float values, you can't just loop (i) times and do System.out.println("%.2f", array[i]);

You can but using System.out.format()

for(int i=0;i<array.length;i++)
{
    System.out.format("%.2f ",array[i]);

}

Output: 12.57 123.12 0.10 41234.21

You could make the array an array of DecimalFormat objects that you could use to format your doubles.

import java.text.DecimalFormat;
public static void main(String args[])  {

    //formatting numbers upto 2 decimal places in Java
    DecimalFormat df = new DecimalFormat("#,###,##0.00");
    System.out.println(df.format(364565.14));
    System.out.println(df.format(364565.1454));

}

You could also create a new object and override the toString method to loop through the array format each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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