I am trying to allow a user to set the number of zeros that come after the decimal point.
I have been working at this for a while now and I can't get it figured out.
I already have an existing Rational class that I wrote. Here is my RationalTester class.
I tried to post the code, but it is too long (exceeds 10000 characters). I have attached the file. I almost broke the program up into two seperate postings, but I didn't know if that was a good idea or not.
Make sure that you keep your DecimalFormat objects and your decimalPlaces variables in sync. For example:
Code:
public class Tester {
private int decimalPlaces;
private DecimalFormat decimalFormat;
public void setNumberOfDigits(int nod) {
decimalPlaces = nod;
String format = "#.";
while( nod-- > 0 ) {
format = format + "0";
}
decimalFormat = new DecimalFormat(format);
// after you call this function, make sure you update the numbers that you are printing.
}
}
Sorry, the "-- >" isn't an "arrow" it is "nod--" is greater than 0. The "--" is shorthand for decrement variable after returning. For example:
Code:
int i = 0; // Before After Value
i++; // 0 1 0
++i; // 1 2 2
i--; // 2 1 2
--i; // 1 0 0
The variable "format" is a temporary that is used to create the pattern for displaying numbers. It should be declared inside the function as shown in the example. "decimalFormat" should be declared as a class member.
I couldn't figure out what some of the code was trying to do. But it didn't seem like you were actually using the DecimalFormat object. Here's the code that I changed:
Code:
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.lang.*;
import javax.swing.*;
public class DecimalFormatTester extends JFrame
{
private int decimalPlaces;
private DecimalFormat decimalFormat;
private String userDigits = "";
private JTextField decimalFormatField;
private String output = "";
JTextArea outputArea;
public DecimalFormatTester()
{
super( "Using Decimal Format" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
outputArea = new JTextArea();
JLabel decimalFormatLabel = new JLabel( " Decimal Points" );
decimalFormatField = new JTextField( 10 );
// Text fields don't post action events, so try binding on key events.
decimalFormatField.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if( e.getKeyCode() == KeyEvent.VK_ENTER ) {
System.out.println("Here");
setNumberOfDigits( Integer.parseInt( decimalFormatField.getText() ) );
output = decimalFormat.format(Math.PI);
displayOutput();
// I don't know what you are trying to do here
/*
String decimalPlacesArray[] = new String[ decimalPlaces ];
for( int counter = 0; counter < decimalPlacesArray.length; counter++ )
{
decimalPlacesArray[ counter ] = "0";
}
String decimalString = "";
for( int counter = 0; counter < decimalPlacesArray.length; counter++ )
{
decimalString += decimalPlacesArray[ counter ];
}
userDigits = "." + decimalString;
setNumberOfDigits( decimalPlaces );*/
}
}
});
container.add( decimalFormatLabel );
container.add( decimalFormatField );
container.add( outputArea );
setSize(420, 125 );
setVisible( true );
}
public void setNumberOfDigits(int nod)
{
decimalPlaces = nod; String format = "#.";
while( nod-- > 0 )
{
format = format + "0";
}
decimalFormat = new DecimalFormat(format);
// after you call this function, make sure you update the numbers that you are printing.
}
private void displayResult( String operation )
{
output += "uD1 " + decimalPlaces;
displayOutput();
}
private void displayOutput()
{
outputArea.setText( output );
}
public static void main( String args[] )
{
DecimalFormatTester dft = new DecimalFormatTester();
dft.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
OK, please let me know if I am understanding this correctly.
DecimalFormat uses patterns.
In the code that you posted, the pattern is "#.".
So the # is the number that the user enters.
Is that right?
How do I use this to apply it to a sum of two fractions or something similar?
I am wanting to be able to let a user input two rational numbers (two numerators and two denominator), and enter the number of decimal places they prefer to see. Then the user can add, subtract, multiply, or divide the two fractions. I have everything working except for this part. I just want to understand how this works. Sorry for all the questions.
Why don't you have look at the documentation for DecimalFormat ? All patterns are
explained there. E.g. '#' is the pattern for one digit that displays as <blank> when absent.
'####.###' would display PI as '3,14'
'0000.000' would display PI as '0003,141'
You just need the code for building the pattern string based on the users input.
As for your last question you need two textfields and a set of radiobuttons
for selecting the operator.
PS: there is a definite limit to how precise a decimal number can be displayed when
using standard mathematical operators for calculation.
Bookmarks