• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

how to subtract decimal????

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
rdy1437
Asked:
rdy1437
  • 7
  • 4
  • 3
  • +1
1 Solution
 
objectsCommented:
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
 
rdy1437Author Commented:
can you provide sample code for this??
0
 
objectsCommented:
BigDecimal a = new BigDecimal("50.56");
BigDecimal b = new BigDecimal("25");
BigDecimal c = a.subtract(b);
System.out.println(c);


0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
objectsCommented:
Floating point numbers are only an approximation (albeit quite an accurate one).
0
 
girionisCommented:
 There is also an excellent tutorial on BigDecimal: http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
0
 
vogenCommented:
// 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
 
vogenCommented:
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
 
objectsCommented:
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
 
rdy1437Author Commented:
thanks guys for your comments.i already used the comment made by objects...
0
 
objectsCommented:
Good to hear it helped :-)
Can you accept my comment as an answer then please.
0
 
vogenCommented:
solly, i was only trying to help.. ::(
0
 
rdy1437Author Commented:
hey objects how can i give you the points?
0
 
objectsCommented:
reject the proposed answer, and accept my comment as an answer :)
0
 
objectsCommented:
0
 
vogenCommented:
I spent my time and this is what I get
it's not fair
thanks anyway
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now