Reputation: 4601
I have a SQLDB with table of field REAL. In my layout I have set its field size to allow only 8+2 digits. In my object I have used the data-type of the field as "float".
qs[0] = "CREATE TABLE " + RELATION_TABLE_NAME + "(id TEXT PRIMARY KEY, name TEXT NOT NULL, startBal REAL NOT NULL, currentBal REAL NOT NULL);";
<EditText android:text="" android:id="@+id/relCurrBalTxt" style="@style/EditTextStyle"
android:inputType="numberDecimal" android:maxLength="11" android:hint="12345678.99" />
I entered value "87654321.99" in my editText and clicked save. It populates the object
// Send the data to object
rowData.setValue(2, Float.parseFloat(sbalTxt.getText().toString()));
// In object, this is how I save it
this.setCurrentBalance( ((Float)value).floatValue() ); // value is Object type
// Saving data in ContenteValues to save to DB
// LOG Rcvd from Object while Saving CurrBal: 8.765432E7
values.put("currentBal", new Float(rowData.getValue(3).toString()));
On saving, it directly show the table with the updated data. While showing it in table I use DecimalFormat to make sure it is shown in proper manner :
field_type = r.getFieldType(field);
// Get Data
str = r.getValue(field).toString();
// Format accordingly
if(field_type.equals(DBRow.DOUBLE_TYPE) || field_type.equals(DBRow.FLOAT_TYPE)) {
double douValue = Double.parseDouble(str);
//NumberFormat nf = NumberFormat.getNumberInstance(Locale.ITALY);
//DecimalFormat df = (DecimalFormat) nf;
DecimalFormat df = new DecimalFormat();
df.applyPattern("##,###,###.##");
df.setGroupingUsed(true);
str = df.format(douValue);
// Log.v("DF", "While Showing in Table : double = " + douValue + " String = " + str);
}
((TextView) tr.getChildAt(field)).setText(str);
HERE it showed me the value : 8.765432E7
When I again selected that row to see the values in EditText I see : 87654320.00
How is the value changed ? In other instance also I saved the data as "50009876.99", but somehow it doesn't save .99 and makes that as .00 i.e 50009876.00.
Why things are not working correctly ? Where am I going wrong ?
Any help is highly appreciated.
*EDIT : DecimalFromat code used to display in table *
// Format accordingly
if(field_type.equals(DBRow.DOUBLE_TYPE) || field_type.equals(DBRow.FLOAT_TYPE)) {
/*
WAS USING THIS CODE TILL NOW
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
String decep = String.valueOf(dfs.getDecimalSeparator());
int index = str.indexOf(decSep);
if (index > 0) {
// If digits after decimal are more than 2
if (str.substring(index+1).length() > 2) {
str = str.substring(0, index+3);
}
}
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(true);
str = df.format(Double.parseDouble(str));
if (addRow)
create_addTextView(tr, str, true);
else
((TextView) tr.getChildAt(field)).setText(str);
*/
// DECIMALFORMAT CODE
double douValue = Double.parseDouble(str);
DecimalFormat df = new DecimalFormat();
df.applyPattern("##,###,###.##");
df.setGroupingUsed(true);
str = df.format(douValue);
if (addRow)
create_addTextView(tr, str, true);
else
((TextView) tr.getChildAt(field)).setText(str);
}
Upvotes: 3
Views: 11668
Reputation: 46963
I think your problem is that you store the numbers as Float
. Float
does not have very good precision - it actually has only 23 binary digits of precision as seen here. This means that you can not store accurately in float
7 digits before the decimal point or even worse - 7 digits before the point and a couple after. This means that you have incorrectly chosen the type of the balance variable - you can not store in Float
8+2. I would recommend you to change the type to Double
, which has significantly larger range of precision.
Upvotes: 6