Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid/SQLite : Saving/Retrieving data of type REAL
    primarykey
    data
    text
    <p>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".</p> <pre><code>qs[0] = "CREATE TABLE " + RELATION_TABLE_NAME + "(id TEXT PRIMARY KEY, name TEXT NOT NULL, startBal REAL NOT NULL, currentBal REAL NOT NULL);"; &lt;EditText android:text="" android:id="@+id/relCurrBalTxt" style="@style/EditTextStyle" android:inputType="numberDecimal" android:maxLength="11" android:hint="12345678.99" /&gt; </code></pre> <p>I entered value "87654321.99" in my editText and clicked save. It populates the object </p> <pre><code>// 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())); </code></pre> <p>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 :</p> <pre><code> 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); </code></pre> <p>HERE it showed me the value : 8.765432E7</p> <p>When I again selected that row to see the values in EditText I see : 87654320.00</p> <p>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.</p> <p>Why things are not working correctly ? Where am I going wrong ?</p> <p>Any help is highly appreciated.</p> <p>*<em>EDIT : DecimalFromat code used to display in table *</em> </p> <pre><code> // 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 &gt; 0) { // If digits after decimal are more than 2 if (str.substring(index+1).length() &gt; 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload