954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java- Round values in a matrix

1. your-netid_hw5_1.java : Liang page 253, programming exercise 7.6.
(Multiplying two matrices) Write a method to multiply two matrices. The header of the method is as follows:
public static double[][] multiplyMatrix(double[][] a, double[][] b)
To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Each element cij is ai1 x b1j + ai2 x b2j + … + ain x bnj.
Write a test program that prompts the user to enter tow 3 x 3 matrices and displays their product.[/COLOR]

So this was the homework problem, and this is the code I have. And it works. The only problem is the output needs to be rounded, I don't know how to do that. Help???

import java.util.Scanner;
public class MultiplyMatrix {

public static void main(String[]args){

System.out.println("Enter numbers continuously for a 3 x 3 matrix: ");
Scanner input = new Scanner(System.in);
double [][] matrix1 = new double[3][3];
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
System.out.print("Enter a number: ");
matrix1[r][c] = input.nextDouble();
}
}

System.out.println("Now enter numbers continuously for second 3 x 3 matrix: ");
double [][] matrix2 = new double [3][3];
for (int g = 0; g < 3; g++)
{
for (int d = 0; d < 3; d++)
{
System.out.print("Enter a number: ");
matrix2[g][d] = input.nextDouble();
}
}

multiply(matrix1, matrix2);
}

public static double[][] multiply(double a[][], double b[][]) {
int m1rows = a.length;
int m1cols = a[0].length;
int m2cols = b[0].length;

double[][] result = new double[m1rows][m2cols];

for (int i=0; i<m1rows; i++)
for (int j=0; j<m2cols; j++)
for (int k=0; k<m1cols; k++){
result[i][j] += a[i][k] * b[k][j];
}

printMatrix(result);
return result;
}

static void printMatrix(double[][] mat) {
int M = mat.length;
int N = mat[0].length;

for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print( mat[i][j] + " ");
}
System.out.println();
}
}

}

The test problem asks you to input
1 2 3
4 5 6
7 8 9

and

0 2 4
1 4.5 2.2
1.1 4.3 5.3
This is what it outputs:

5.300000000000001 23.9 24.0
11.600000000000001 56.3 58.2
17.9 88.69999999999999 92.4

How can i round those values????

alliswim2010
Newbie Poster
11 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

use:

double x=0.555;
System.out.println(Math.round(x));

to round a double easily.

teo236
Junior Poster in Training
73 posts since Jul 2011
Reputation Points: 20
Solved Threads: 10
 

Or see the String class's format() method.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Humm i'm not sure I understand. I know to how round numbers. But how would i round
[CODE] result[i][j] += a[i][k] * b[k][j];/CODE]

alliswim2010
Newbie Poster
11 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Do you want to round the value in the result?
Vs rounding the value when it is printed?
For example if result = 1.555543
Do you want to change the contents of result to 1.560000?
or leave the value in result unchanged and only round it when printing?

How many decimal places do you want to round to?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Sorry I should have specified. I need all the values to be rounded to two decimal places. So for example 88.69999999999999 should be 88.69 or 88.70

alliswim2010
Newbie Poster
11 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Are you referring to the values in the variables or the Strings that are printed?
The values in the variables will always have the same significance unless you change them.
To round a double to the second decimal place, add .005, multiply by 100, convert to int and divide by 100 back into the double.
For example:

double dbl = 96.567899999;
      int rnded = (int)((dbl + .005) * 100);    // round to 2 decimal places
      double dblRnded = rnded/100.0;
      System.out.println("dbl=" + dbl + ", dblRnded=" + dblRnded); // dbl=96.567899999, dblRnded=96.57
for example 88.69999999999999 should be 88.69 or 88.70


The first: 88.69 is truncated, the second is rounded

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Okay. I know how to round a single value. My issue is that the final method that i call is PrintMatrix, and it prints the "result" matrix I used in the Multiply method. I need to figure out how to round ALL the numbers in that matrix so they only have two decimal places. Its a 3x3 matrix and it outputs this.
5.300000000000001 23.9 24.0
11.600000000000001 56.3 58.2
17.9 88.69999999999999 92.4

I need the program to return the matrix to look a little more cleaned up. So how would i make the entire matrix rounded and not just one number.

alliswim2010
Newbie Poster
11 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

For instance instead of this:
5.300000000000001 23.9 24.0
11.600000000000001 56.3 58.2
17.9 88.69999999999999 92.4
I need this:
5.3 23.9 24.0
11.6 56.3 58.2
17.9 88.7 92.4

alliswim2010
Newbie Poster
11 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
how to round ALL the numbers
how would i make the entire matrix rounded and not just one number.


Write code that takes each number in the array and formats it the way you want.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

@alliswim2010

please read this thread

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

Another solution would be to use the DecimalFormat class.

public static void main(String[] args) throws Exception {
	double[] doubles = {5.300000000000001, 123231.600000000000001, 88.695555555 };
	final DecimalFormat fmt = new DecimalFormat(".00");
	for (double d : doubles) {
		System.out.println(fmt.format(d));
	}
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Use the DecimalFormat class.

public static void main(String[] args) throws Exception {
	double[] doubles = {5.300000000000001, 123231.600000000000001, 88.695555555 };
	final DecimalFormat fmt = new DecimalFormat(".00");
	for (double d : doubles) {
		System.out.println(fmt.format(d));
	}
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: