# Confused with decimal

• 05-21-2005, 09:46 PM
Kinda Electroni
Confused with decimal
Hi all,
I am running this program, it compile and run.
What I am getting is not what I want.
I want it to print only 2 decimal points.
How can I make print only two dec. instead of 15.

public class array{
public static double[][] twoDDoubleArray(
int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
double increment = (valEnd - valStart)/(xLen * yLen);
double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
array[i][j] = val;
val += increment;
}
return array;
}
public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void main(String args[]) {
double[][] twoD = twoDDoubleArray(4, 6, 47.0, 99.0);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
System.out.println("**********************");
double[][] twoD3 = twoDDoubleArray(9, 5, 47.0, 99.0);
printArray(twoD3);
}
} //
• 05-22-2005, 06:44 AM
sjalle
Use java.text.DecimalFormat
• 05-22-2005, 10:09 AM
Kinda Electroni
Quote:

Originally Posted by sjalle
Use java.text.DecimalFormat

What is java.txt? :cool:
• 05-22-2005, 10:50 AM
sjalle
Its a package in Sun's java SDK (what that is, is my little secret :) ).
Many formatting classes are contained in this package.

Code:

```static DecimalFormat dcf=new DecimalFormat("0.00"); double d=1.345678d; System.out.println(dcf.format(d));```
• 05-22-2005, 11:36 AM
Kinda Electroni
Quote:

Originally Posted by sjalle
Its a package in Sun's java SDK (what that is, is my little secret :) ).
Many formatting classes are contained in this package.

Code:

```static DecimalFormat dcf=new DecimalFormat("0.00"); double d=1.345678d; System.out.println(dcf.format(d));```

I will try it, even though, it prits line and I am dealing with array.
Let me see.
• 05-22-2005, 11:53 AM
sjalle
Ehh, no it doesn't print anything, it returns a string containing a formatted double value.

static DecimalFormat dcf=new DecimalFormat("0.00");
.
.
System.out.print(" " + dcf.format(array[i][j]));
• 05-22-2005, 11:56 AM
Kinda Electroni
I've tried this one.
It does not compile.

public class Dcf {
public static void main(String args[]) {
Static DecimalFormat dcf=new DecimalFormat("0.00");
double d=1.345678d;
System.out.println(dcf.format(d));
}
}
• 05-22-2005, 12:01 PM
sjalle
static variables cannot be defined inside methods, they are class global, and...
no capital "S", just static

Code:

```import java.text.*; public class DCF {  static DecimalFormat dcf=new DecimalFormat("0.00");  public static void main(String args[]) {     double d=1.345678d;     System.out.println(dcf.format(d));   } }```
• 05-22-2005, 12:25 PM
Kinda Electroni
Quote:

Originally Posted by sjalle
static variables cannot be defined inside methods, they are class global, and...
no capital "S", just static

Code:

```import java.text.*; public class DCF {  static DecimalFormat dcf=new DecimalFormat("0.00");  public static void main(String args[]) {     double d=1.345678d;     System.out.println(dcf.format(d));   } }```

Yes, it just seems like it...
It works.
Thank you. :WAVE: