0

If I start my android app the app calls some data from a DB and save this data on the local SQL DB on the device. There is one value with a big number so I need BigInteger to store it. If I try to call that value from the local DB I get the error:

java.lang.NumberFormatException: Invalid BigInteger: 3.23431e+23

This is how I store the value in the local db:

...
values.put(BatteryEntryContract.BatteryEntry.COLUMN_NAME_SERVICE_TAG, String.valueOf(battery.get(i).getServiceTag()));
...
db.insert(BatteryEntryContract.BatteryEntry.TABLE_NAME_BATTERY, null, values);

This is how I call it from the local DB:

...
String serviceTagValue = cursor.getString(
                    cursor.getColumnIndexOrThrow(BatteryEntryContract.BatteryEntry.COLUMN_NAME_SERVICE_TAG));
batteryFromDb.setServiceTag(new BigInteger(serviceTagValue));
...

Do I have to format the value from the DB in a special way that I get in full length and not in the way with ...e+23?

EDIT: This is how the column type will be set:

...
BatteryEntry.COLUMN_NAME_SERVICE_TAG + " TEXT DEFAULT 0," +
....

When I try

...
values.put(BatteryEntryContract.BatteryEntry.COLUMN_NAME_SERVICE_TAG, 
String.valueOf(battery.get(i).getServiceTag()));
...
Log.i("---",values+"");
db.insert(BatteryEntryContract.BatteryEntry.TABLE_NAME_BATTERY, null, values);

The value in the external DB is saved as numeric(24,0) when I get it from there I save it as String in the local DB. when I do Log.i("---",values+""); ("values" will be saved in the local DB) the number will be displayed correct:

.... serviceTag=323431303230313631333331 ....
WeSt
  • 889
  • 5
  • 14
  • 32
  • `Invalid BigInteger: 3.23431e+23` because `3.23431e+23` is a **string representation** of a number. – Phantômaxx Aug 01 '17 at 15:33
  • But how can I call the BigInt value? cursor.getBigInt is not possible – WeSt Aug 01 '17 at 15:51
  • Simply use `cursor.getLong()` – Phantômaxx Aug 01 '17 at 16:22
  • This doesn't work because I need to store a BigInt with 24digits. This is to larg for getLong() – WeSt Aug 02 '17 at 06:30
  • Then store a string. – Phantômaxx Aug 02 '17 at 06:50
  • Thas what I did – WeSt Aug 02 '17 at 06:51
  • But then you have to **cast** the string to BigInt in your app. And **re-cast** it to string before inserting it in the db. – Phantômaxx Aug 02 '17 at 06:55
  • The value in the external DB is saved as numeric(24,0) when I get it from there I save it as String in the local DB. when I do `Log.i("---",values+"");` ("values" will be saved in the local DB) the number will be displayed correct: `.... serviceTag=323431303230313631333331 ....` – WeSt Aug 02 '17 at 07:14
  • So... what's the problem? – Phantômaxx Aug 02 '17 at 07:18
  • When I try to get the Value from the local DB as a String and cast it to BigInt I get the problem from my first post – WeSt Aug 02 '17 at 07:20
  • Then, read my first comment: `3.23431e+23` is **not a number**. You have to **cast** this string to a number. – Phantômaxx Aug 02 '17 at 07:35
  • 1
    @ModularSynth: Like your profile image. I built (Elektor Formant) modular synths and digital sequencers and some other peripherals way back then and am now trying to emulate those in software. I once played with a Doepfler A-100 in The Music Store in Cologne, and I even managed to get it working. Yes, I know this is off-topic. – Rudy Velthuis Aug 02 '17 at 10:29
  • @RudyVelthuis Rudy, my friend. You'll be happy to know that there already exists Softube Modular (about 80€ by Thomann). It's made in collaboration with Doepfer and it emulates nearly all Doepfer modules and a selection of Intellijel ones. And it's ever growing. – Phantômaxx Aug 02 '17 at 11:10
  • @ModularSynth: Nah, thanks but no thanks, I want to write my own standalone copy of my own old synths (I sold those many years ago, when I moved to Germany). That's the entire fun of it. It is a pet project. – Rudy Velthuis Aug 02 '17 at 11:12
  • @RudyVelthuis I', so badly curious to see your modules. Are they in Eurorack format? Do you have videos on YouTube? I'll subscribe to your channel! – Phantômaxx Aug 02 '17 at 11:14
  • 1
    I only have a simple working copy (in software). Only VCO, VCF, VCF24dB, ADSR, LFO, VCA and some logic at the moment. No really exchangeable modules yet, just a fixed setup and a few switching possibilities. – Rudy Velthuis Aug 02 '17 at 11:15
  • But... something I overlooked till now: Do you really need that awful lot of decimal precision?! only to store a battery level? – Phantômaxx Aug 02 '17 at 12:31
  • It's a RFID Tag which need to be unique to each battery – WeSt Aug 02 '17 at 12:33
  • A tag... then it is a STRING. It doesn't have to be a BigInt at all. – Phantômaxx Aug 02 '17 at 12:51
  • I know it would be easy to it with a string but it's a Customer specification to handle it as a numeric value – WeSt Aug 02 '17 at 12:55
  • Customers aren't typically the smartest people to deal with. Most the times they ask you the moon, but you can't give it to them. – Phantômaxx Aug 02 '17 at 14:09

1 Answers1

0

What happens if you do:

String serviceTagValue = cursor.getString(
                cursor.getColumnIndexOrThrow(BatteryEntryContract.BatteryEntry.COLUMN_NAME_SERVICE_TAG));
BigDecimal bdec = new BigDecimal(serviceTagValue);
batteryFromDb.setServiceTag(bdec.toBigInteger());
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • It's the same error `java.lang.NumberFormatException: Invalid BigInteger: 3.23431E+23` – WeSt Aug 02 '17 at 06:27
  • 1
    That doesn't make sense. Did you really use BigDecimal? – Rudy Velthuis Aug 02 '17 at 06:32
  • Sorry had a typing error. But this method made the number: `323431303230313631333334` to `323431000000000000000000` – WeSt Aug 02 '17 at 06:39
  • 1
    Then it was not saved with the correct precision. Inspect the string that comes out of `cursor.getString()`. If it is `"3.23431E+23"` then you perhaps saved it in the wrong format. `"3.23431E+23"` is the same as `3234310000...`. If you need better precision, save it as such, e.g. as string and not as, say, float. – Rudy Velthuis Aug 02 '17 at 07:52
  • Please see my EDIT in the first post. Maybe there is a error which I don't see – WeSt Aug 02 '17 at 08:01
  • If so, then what is the value of the string you retrieve? – Rudy Velthuis Aug 02 '17 at 12:06
  • With this line `String serviceTagValue = cursor.getString( cursor.getColumnIndexOrThrow(BatteryEntryContract.BatteryEntry.COLUMN_NAME_SERVICE_TAG));` serviceTagValue has the value `3.23431e+23` – WeSt Aug 02 '17 at 12:13
  • So what you get out *before* you pass it to `BigDecimal` is already wrong (as I said, `3.23431e+23` is the same as `323431000000000000000000`). I can't help you there. – Rudy Velthuis Aug 02 '17 at 12:16