-
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.
Thank you in advance.
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);
}
} //
-
Use java.text.DecimalFormat
eschew obfuscation
-
 Originally Posted by sjalle
Use java.text.DecimalFormat
What is java.txt?
-
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));
eschew obfuscation
-
 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.
-
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]));
eschew obfuscation
-
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));
}
}
-
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));
}
}
eschew obfuscation
-
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks