Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to send a float to a TextBox

Status
Not open for further replies.

cal555

Programmer
May 5, 2006
71
US
Hi
I am trying to learn java, because my company is moving to java, and I wrote simple applet that will take an integer multiply it by 2 and send the output to a textbox. This works great, but I cannot find anywhere, how I would do this if it was a float; like if I was computing the tax on an amount Exp. 2 * 1.08 then the textbox would get 2.16. What would I need to change in the program below to do this? Any ideas would really be appreciated.
Thank You
here is the code that I have to handle the Integer.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class taxTotal extends Applet implements ActionListener
{
int amt;
TextField inputAmt;
TextField OutPut;
Label amtlbl;
Label total;
Button Enter;

public void init()
{
amtlbl = new Label("Enter the total without tax");
add(amtlbl);

inputAmt = new TextField( 10 );
add(inputAmt);

total = new Label("total with tax");
add(total);

OutPut = new TextField( 10 );
OutPut.setEditable( false );
add(OutPut);

Enter = new Button("Enter");
Enter.addActionListener(this);
add(Enter);


}


public void actionPerformed(ActionEvent e )
{

computAmt();

}

public void computAmt()
{

int Subtot;
int sum;

sum = Integer.parseInt(inputAmt.getText());

Subtot = sum * 2;

OutPut.setText(Integer.toString(Subtot));

}

}
 
Well,
Code:
 public void computAmt()
  {

    float subTotal;
    float sum;

    sum = Float.parseFloat(inputAmt.getText());

    subTotal = sum * 2;

    OutPut.setText(Float.toString(subTotal ));

  }

or for more precision,
Code:
 public void computAmt()
  {

    double subTotal;
    double sum;

    sum = Double.parseFloat(inputAmt.getText());

    subTotal = sum * 2;

    OutPut.setText(Double.toString(subTotal));

  }

However, where money is concerned, you'll probably want to keep sums to a certain number of decimal-places. This can be done using the java.math.BigDecimal class which has the concept of decimal-place precision. It's a bit slow and unwieldy, though. There are slimmer, faster versions available as Open Source libraries on the internet.

Alternatively, you could hold your values un-rounded as doubles, and just display them with the precision you want. Check out the java.text.NumberFormat class and specifically the java.text.DecimalFormat class.

Tim
 
It's
Code:
Double.parseDouble()
and you can also use
Code:
String.valueOf()
to obtain a String representation regardless what kind of number you're using.

Cheers,
Dian
 
Thanks Dian. Perils of posting before the first coffee of the morning.

Code:
Double.parseFloat(inputAmt.getText())
should have been
Code:
Double.parseDouble(inputAmt.getText())

Tim
 
Thanks everyone for your help, I will try this when I get home. I really appreciate your help I do not know why I could not find this in my books or on the net.
Thanks
 
Well, just by typing java float to string into Google, the sollution comes up.

Cheers,
Dian
 
I have terrible luck finding stuff via Google (or even Search on PHP.net), so you're not alone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top