[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 1246 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 90 days
Recommended Popular Threads Controvertial Threads
What are your first memories of when Nepal Television Began?
निगुरो थाहा छ ??
Basnet or Basnyat ??
TPS Re-registration case still pending ..
Sajha has turned into MAGATs nest
NRN card pros and cons?
Will MAGA really start shooting people?
मन भित्र को पत्रै पत्र!
Top 10 Anti-vaxxers Who Got Owned by COVID
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
TPS Work Permit/How long your took?
Breathe in. Breathe out.
3 most corrupt politicians in the world
Guess how many vaccines a one year old baby is given
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
चितवनको होस्टलमा १३ वर्षीया शालिन पोखरेल झुण्डिएको अवस्था - बलात्कार पछि हत्याको शंका - होस्टेलहरु असुरक्षित
शीर्षक जे पनि हुन सक्छ।
Disinformation for profit - scammers cash in on conspiracy theories
someone please tell me TPS is here to stay :(
Travelling to Nepal - TPS AP- PASSPORT
Nas and The Bokas: Coming to a Night Club near you
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