Decimal places (too many)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Decimal places (too many)

  1. #1
    Join Date
    Jan 2005
    Posts
    61

    Decimal places (too many)

    I'm trying to calculate the tax payable on sales, and want to reduce the amount of decimal places in the answer.

    I'm using doubles for the cost price, sale price and tax calculation, but it's being too accurate - I want an answer like 10.27, but instead I'm getting 10.4354356437896 or something similar.

    Is there an easy way to cut this down when I set it in a text field as a String, or should I be using floats for the calculation?

    Thanks

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Here's an example:

    Code:
    import java.text.*;
    
    public class Test {
        public static void main(String[] args) {
            double d = 3.14159265;
            DecimalFormat df_1 = new DecimalFormat("0.00");
            DecimalFormat df_2 = new DecimalFormat("0.0000");
            String format_1 = df_1.format(d);
            String format_2 = df_2.format(d);
            System.out.println(format_1+"\n"+format_2);
        }
    }

  3. #3
    Join Date
    Jan 2005
    Posts
    61
    Thanks for the help - that did it

  4. #4
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by will808
    Thanks for the help - that did it
    Good to hear!

Similar Threads

  1. Currency and forcing Decimal Places
    By Damien in forum ASP.NET
    Replies: 3
    Last Post: 08-18-2002, 05:00 PM
  2. Conversion of decimal in Oracle to Access 2000
    By Villy Olsson in forum Database
    Replies: 4
    Last Post: 04-06-2002, 09:01 PM
  3. Numeric decimal places in ADO control
    By rkbnair in forum VB Classic
    Replies: 0
    Last Post: 01-31-2002, 01:48 PM
  4. Calculated value and decimal places
    By Jim C. in forum Database
    Replies: 2
    Last Post: 01-03-2001, 09:12 AM
  5. change decimal places value
    By rh in forum VB Classic
    Replies: 3
    Last Post: 07-27-2000, 08:36 AM

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