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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you set the value of a Float, aside from using 'new'? 2

Status
Not open for further replies.

tomstra

Programmer
Oct 30, 2000
6
US
This is a very simple question.
I know I can set the value of a Float at constructor time. E.g.,

Float myFloat = new Float(accum); (where accum is a float)

but once myFloat has been created, how can I change its value? E.g., suppose the value of accum has changed and now I want to set the value of myFloat to accum (w/o having to re-construct it). I would think you could do something like

myFloat.setValue(accum); (but you can't, Floats don't have a setValue method)

How would you do this?

Thanks for any help!
 
if accum is a float then just assign it the new value using:

myFloat = assum;

Provided I think, assum was created with the constructor too, and is not a primitive. Just have to use slightly different methodology to get what you want - so you just need to match whether they are objects or primitives - conversion is easy. If assum was primitive you would use something like:

myFloat = new Float(assum);

and to get a float, from a Float object use the the toFloat method:

Float someFloat = new Float(2.13);
float sameFloatPrimitive = someFloat.floatValue();

Hope this helps ;-Q
b[sup]2[/sup] - benbiddington@surf4nix.com
 
Dear Bangers,
Thank you for your reply.
Actually accum is a primitive, so I can't do the myFloat = accum;

Consider this:

Float myFloat = new Float(textbox.getText()); // get number to be added
accum = accum + myFloat.floatValue(); // add to running total

// How do I load accum into myFloat so that I can display the new value of accum in
// textbox? (Would I re-issue a 'new' on myFloat, e.g.,
// Float myFloat = new Float(accum);
// even though I just said Float myFloat = new Float 2 lines ago?

textbox.setText(myFloat.toString()); // display new total in textbox

Thanks!
 
perhaps you can use this:

try
{
accum += Float.parseFloat(textbox.getText());
}
catch(NumberFormatException)
{
System.out.println("No real float in textbox");
}
textbox.setText(""+accum); //instant conversion from float to String

Hope this helps,
Steven.
 
Thanks for the tip! I'll give it a shot. -Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top