Solved

how to subtract decimal????

Posted on 2002-05-27
15
356 Views
Last Modified: 2008-02-26
hi guys!!!!

i had a program that subtract two double variable.here's my code:



import java.awt.*;
import java.awt.event.*;

public class Try
{

        public static void main(String argv[])
    {
            Frame fr = new Frame();
            fr.pack();
            fr.setVisible(true);

            Panel panel = new Panel();

            final TextField tf1 = new TextField(15);
            final TextField tf2 = new TextField(15);

            final Button subtract = new Button("Minus");
            Button clear = new Button("Clear");
            subtract.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                         if (e.getSource() == subtract)
                         {
                              double a = new Double(tf1.getText()).doubleValue();
                              double b = new Double(tf2.getText()).doubleValue();

                              System.out.println(a-b);
                         }
               }

          });

            panel.add(tf1);
            panel.add(tf2);
            panel.add(subtract);
            panel.add(clear);

            fr.add(panel);
       }//End of main

       }


OUTPUT:

TF1 = 50.56
TF2 = 25
DIFFERENCE = 25.560000000000002


how would i make it so that it will only return 25.26???

thanks in advance!!!




0
Comment
Question by:rdy1437
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7036647
That's simply a rounding error resulting from floating point arithmetic. If you need to avoid this then use the BigDecimal class, or simply format your output to two decimal places.
0
 

Author Comment

by:rdy1437
ID: 7036689
can you provide sample code for this??
0
 
LVL 92

Expert Comment

by:objects
ID: 7036709
BigDecimal a = new BigDecimal("50.56");
BigDecimal b = new BigDecimal("25");
BigDecimal c = a.subtract(b);
System.out.println(c);


0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 92

Expert Comment

by:objects
ID: 7036713
Floating point numbers are only an approximation (albeit quite an accurate one).
0
 
LVL 35

Expert Comment

by:girionis
ID: 7036722
 There is also an excellent tutorial on BigDecimal: http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
0
 
LVL 2

Expert Comment

by:vogen
ID: 7039576
// rdy1437, here is the solution:

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class Try
{
     public static void main(String argv[])
  {
       Frame fr = new Frame();
    fr.pack();
    fr.setVisible(true);
    Panel panel = new Panel();

    final TextField tf1 = new TextField(15);
    final TextField tf2 = new TextField(15);

    final Button subtract = new Button("Minus");
    Button clear = new Button("Clear");
    subtract.addActionListener(new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
     {
      DecimalFormat twoDigits = new DecimalFormat( "0.00" );
      if (e.getSource() == subtract)
      {
        double a = new Double(tf1.getText()).doubleValue();
        double b = new Double(tf2.getText()).doubleValue();
                 double result =(double) a - (double) b;
                    System.out.println( twoDigits.format(result) );
      }
   }
   });
   panel.add(tf1);
   panel.add(tf2);
   panel.add(subtract);
   panel.add(clear);
   fr.add(panel);
   }//End of main
}
0
 
LVL 2

Expert Comment

by:vogen
ID: 7039589
DecimalFormat object that is initialized with the string "0.00". Each 0 is a format flag that specifies a required digit position in the formatted floating-point number.

pls ask if have any question..

cheers
0
 
LVL 92

Expert Comment

by:objects
ID: 7040541
vogen,

1. Please avoid proposing answers as it locks the question and make it harder for other experts to contribute.

2. I had already suggested the option of using formatting.
0
 

Author Comment

by:rdy1437
ID: 7040694
thanks guys for your comments.i already used the comment made by objects...
0
 
LVL 92

Expert Comment

by:objects
ID: 7040714
Good to hear it helped :-)
Can you accept my comment as an answer then please.
0
 
LVL 2

Expert Comment

by:vogen
ID: 7043053
solly, i was only trying to help.. ::(
0
 

Author Comment

by:rdy1437
ID: 7043417
hey objects how can i give you the points?
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7043427
reject the proposed answer, and accept my comment as an answer :)
0
 
LVL 92

Expert Comment

by:objects
ID: 7043547
0
 
LVL 2

Expert Comment

by:vogen
ID: 7043677
I spent my time and this is what I get
it's not fair
thanks anyway
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse with various Java releases 7 92
swing controls 2 38
DTD and JAVA versions 1 59
Java pass by reference 3 76
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question