Storing float numbers as strings in android database
Posted
by sandis
on Stack Overflow
See other posts from Stack Overflow
or by sandis
Published on 2010-04-27T07:25:09Z
Indexed on
2010/04/27
7:53 UTC
Read the original article
Hit count: 267
So I have an app where I put arbitrary strings in a database and later extract them like this:
Cursor DBresult = myDatabase.query(false, Constant.DATABASE_NOTES_TABLE_NAME,
new String[] {"myStuff"}, where, null, null, null, null, null);
DBresult.getString(0);
This works fine in all cases except for when the string looks like a float number, for example "221.123123123". After saving it to the database I can extract the database to my computer and look inside it with a DB-viewer, and the saved number is correct. However, when using cursor.getString() the string "221.123" is returned. I cant for the life of me understand how I can prevent this. I guess I could do a cursor.getDouble() on every single string to see if this gives a better result, but that feels sooo ugly and inefficient. Any suggestions?
Cheers,
edit: I just made a small test program. This program prints "result: 123.123", when I would like it to print "result: 123.123123123"
SQLiteDatabase database = openOrCreateDatabase("databas", Context.MODE_PRIVATE, null);
database.execSQL("create table if not exists tabell (nyckel string primary key);");
ContentValues value = new ContentValues();
value.put("nyckel", "123.123123123");
database.insert("tabell", null, value);
Cursor result = database.query("tabell", new String[]{"nyckel"}, null, null, null, null, null);
result.moveToFirst();
Log.d("TAG","result: " + result.getString(0));
© Stack Overflow or respective owner