Confused with decimal


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Confused with decimal

Hybrid View

  1. #1
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    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);
    }
    } //

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Use java.text.DecimalFormat
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Quote Originally Posted by sjalle
    Use java.text.DecimalFormat
    What is java.txt?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  5. #5
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    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.

  6. #6
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    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));
    }
    }

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  9. #9
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center