"Formatting" in console apps and DecimalFormat


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: "Formatting" in console apps and DecimalFormat

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    "Formatting" in console apps and DecimalFormat

    Hi, I've written a program that takes values of an angle in degrees, performs some calculations (after converting to radians of course) and then outputs all the data in 4 columns The last column isn't aligned properly (see http://www.zenadsl3101.zen.co.uk/ballistics.JPG). Is there any way to have that column aligned like the others?

    The other thing is that I used a DecimalFormat object to round the calculated values to 4 dp. This is fine for all the values except for X when t = 90 (again, see screenshot). The value printed is 0.0000, which is actually correct (as the program calculates sin(2*tInRadians) multiplied by stuff). However, before using DecimalFormat, the value for X at t = 90 was given as a really small number, of order 10^-15. I assume that this is because when calculating the value of sin(2*tInRadians), it can't take the whole value of π/2 as it's irrational, so some data is lost. What I want to do is have this value rounded to 4dp, so I get 4.xxxxE-15, or something. How can I do this?

    Also, I'm forced to use JDK 1.2.2_10 .

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    This is how I would sove your first problem:

    public class Formatter {
    public Formatter() {
    }
    public static void main(String[] args) {
    DecimalFormat df=null;
    DecimalFormatSymbols dfs=new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');

    df=new DecimalFormat("00000000.0000000", dfs);
    for (int i=0; i<10; i++) {
    double d=1234.56D*Math.random();
    System.out.println(df.format(d));
    }
    }
    }
    An answer to your second question may be here
    eschew obfuscation

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