D
dcMan
Hello,
I'm trying to convert a String to a float, do some arithmetic on it, then
display the new value on a Swing text field. Then do the reverse. Take a
decimal numeric value the operator has entered from the GUI, convert it to a
float do some arithmetic to remove the decimal, convert it back to a String
to save it to a database.
The code works fine until I exceed 8 digits. Beyond 8 digits the float is
stored in memory in scientific notation (found this out by stepping through
using the debugger) so when it is converted to a String I do not get the
correct data. I get something like 1.234567891E7 instead of 12345678.91
I've also tried using a double but I got the same results.
String sWeight = "";
float floatWeight= 0;
//Retrieve weight from database stored as a String values, with no decimals
sWeight = getWeight();
// need to put a two digit decimal place within the number before
displaying, so divide by 100
floatWeight = Float.parseFloat(sWeight) / 100.00;
// convert the float to a string and display on the GUI.
gui_text_field.setText(String.valueOf(floatWeight);
I also need to the same thing in reverse, if the operator manually enters
the weight it must be save back to the database as a String.
String myString = "";
float floatValue = 0;
//Get the text string entered by operator (ex 12345678.91)
myString = gui_text_field.getText();
// move the decimal to the end of the number, so multiply by 100
floatValue = Float.parseFloat(myString) * 100;
// convert the weight to a string
myString = Float.toString((float)floatValue);
// strip out the decimal point
myString = myString.substring(0, myString.indexOf("."));
// save String to the String data column in database, myString should be
equal to 1234567891
saveDataToDatabase(myString);
Thanks in Advance
I'm trying to convert a String to a float, do some arithmetic on it, then
display the new value on a Swing text field. Then do the reverse. Take a
decimal numeric value the operator has entered from the GUI, convert it to a
float do some arithmetic to remove the decimal, convert it back to a String
to save it to a database.
The code works fine until I exceed 8 digits. Beyond 8 digits the float is
stored in memory in scientific notation (found this out by stepping through
using the debugger) so when it is converted to a String I do not get the
correct data. I get something like 1.234567891E7 instead of 12345678.91
I've also tried using a double but I got the same results.
String sWeight = "";
float floatWeight= 0;
//Retrieve weight from database stored as a String values, with no decimals
sWeight = getWeight();
// need to put a two digit decimal place within the number before
displaying, so divide by 100
floatWeight = Float.parseFloat(sWeight) / 100.00;
// convert the float to a string and display on the GUI.
gui_text_field.setText(String.valueOf(floatWeight);
I also need to the same thing in reverse, if the operator manually enters
the weight it must be save back to the database as a String.
String myString = "";
float floatValue = 0;
//Get the text string entered by operator (ex 12345678.91)
myString = gui_text_field.getText();
// move the decimal to the end of the number, so multiply by 100
floatValue = Float.parseFloat(myString) * 100;
// convert the weight to a string
myString = Float.toString((float)floatValue);
// strip out the decimal point
myString = myString.substring(0, myString.indexOf("."));
// save String to the String data column in database, myString should be
equal to 1234567891
saveDataToDatabase(myString);
Thanks in Advance