DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  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

Bookmarks

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


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


Sponsored Links