[Show all top banners]

pat
Replies to this thread:

More by pat
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 java help
[VIEWED 2583 TIMES]
SAVE! for ease of future access.
Posted on 02-17-07 12:28 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hi..everyone...I have created this simple java calculator for addition and subtraction.
Can anyone help me get the result till 40 decimal places when necessary...Right now it works till 15-16 decimal places but how do i change that to 40....
Thanks....


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Object;


public class Calculator extends JFrame implements ActionListener

{
public static final int WIDTH= 400;
public static final int HEIGHT= 175;

private JTextField inputField;
private JTextField inputField1;
private JTextField outputField;
private double sum=0;
private String sumString;
private JLabel label1,label2, label3, label4;

public static void main (String [] args)
{

Calculator guiCalculator = new Calculator();
guiCalculator.setVisible(true);


}
public Calculator()
{
setTitle ("Java Calculator");
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
contentPane.setLayout (new GridLayout());

JPanel textArea = new JPanel();
textArea.setBackground(Color.green);
textArea.setLayout(new FlowLayout());

label1 = new JLabel( "Enter 1st Number Here" );
textArea.add( label1 );

inputField = new JTextField("", 15);
inputField.setBackground(Color.white);
textArea.add(inputField);

label2 = new JLabel( "Enter 2nd Number Here" );
textArea.add( label2 );

inputField1= new JTextField("",15);
inputField1.setBackground(Color.WHITE);
textArea.add(inputField1);

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.GRAY);
buttonPanel.setLayout(new GridLayout());

JButton addButton = new JButton("Add");
addButton.addActionListener(this);
buttonPanel.add(addButton);


JButton resetButton = new JButton("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);


JButton subtractButton = new JButton("Subtract");
subtractButton.addActionListener(this);
buttonPanel.add(subtractButton);


Panel b = new Panel();
b.setLayout(new GridLayout(1,3));
b.add(addButton);
b.add(subtractButton);
b.add(resetButton);
textArea.add(b);
contentPane.add(textArea, BorderLayout.CENTER);
outputField = new JTextField("Result", 30);
outputField.setBackground(Color.WHITE);
textArea.add(outputField);

label3 = new JLabel( "Java Calculator");

textArea.add( label3 );
}

public void actionPerformed (ActionEvent e)
{
try
{
if(e.getActionCommand().equals("Add"))
{
sum = stringToDouble(inputField.getText()) + stringToDouble(inputField1.getText());
String sumString = Double.toString(sum);
outputField.setText(sumString);
}
if(e.getActionCommand().equals("Subtract"))
{
sum = stringToDouble(inputField.getText()) - stringToDouble(inputField1.getText());

String sumString = Double.toString(sum);
outputField.setText(sumString);
}
if (e.getActionCommand().equals("Reset"))
{
sum = 0;
inputField.setText("0.0");
inputField1.setText("0.0");
outputField.setText("0.0");
}
}

catch (Exception exception)
{
sum = 0;
inputField.setText("Enter Only Numbers");
inputField1.setText("Enter Only Numbers");
outputField.setText("0.0");
}
}


private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());

}
}
 
Posted on 02-17-07 12:45 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

does double variable hold a number with 40 decimals ???........... if it does not u can parse the string into 2 or more sub strings and add separately ....... and again concatenate the answer into one string.
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
ए १ पनि पुगेनछ ?
नोबेल शान्ति पुरस्कार र अशान्त राष्ट्रपतिको बालहठ
200 denaturalization cases per month to the Department of Justice for the 2026 fiscal year.
मिरो प्रेडिक्शन जन्मेर एमेरिकामा आखा खो ल न पायेका नागरिकता बारे
मानसिक सन्तुलन, एक कहालीलाग्दो घटना सिक्नुपर्ने कुराहरु
Breaking News: Ninth Circuit Rejects Government Bid to Undo Nepal TPS Order, Leaves Protections in Place
Sukulgunda 2.0 / www.sukulgunda.com
Why Prachanda seems to be the smartest of them all?
When will Nepali women be equal?
बालेंन मेयर बाट प्रधान मन्त्रि हुने भो ?
When will the culture of impunity end?
बालेंन आए पछि आशाका किरण देखिन थालेका छन् !!
Can we really get rid of Nepotism in the government?
आरको नेपालीले बेजत गर्यो फेरी अरविंग टेक्सासमा बुढ़ाहरू लाई स्कॉम गरेर
How many ministries do we really need?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters