I am attempting to enter values such as “0.20” within a table but when I display it within an activity it shows “0.2”. I am uncertain as to why the ‘0’ is being removed i.e. incorrect structure of the table, insertion, or being returned.
My table is created as followed:
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_SWIMMERLAPS + " INT NOT NULL, "
+ KEY_SPONSOR + " DEC(4,2) NOT NULL );");
I then insert the data:
public long addSwimmer(String name, String laps, String sponsor) {
int i = Integer.parseInt(laps);
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SWIMMERLAPS, i);
cv.put(KEY_SPONSOR, (new DecimalFormat("0.00##")).format(0.20));
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
and then return the sponsorship data:
public String getSwimmerSponsors() {
String[] columns = new String[] { KEY_SPONSOR };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, KEY_SWIMMERLAPS + " DESC");
String result = "";
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(0) + "\n";
}
return result;
}
Edit – I display the returned result in my layout as follows:
String sponsors = swimmerDb.getSwimmerSponsors();
tvSponsor.setText(sponsors);
Thanks.
Because you’re displaying an unformatted Java variable, and leading/trailing 0s are suppressed.
If you want to format it with a specific layout, use one of the
printf/formatvariants: