I am working on a spring boot project with JPA where i am facing an issue.
From UI i will get a string of 4 length and i have to append 4 zeros as prefix and save it in a table. The data type of this column in entity is Double.
eg, String from UI is 1234, then i have to make it as 0.00001234 and insert into a table.
Double d = new Double("0.00001234");
after inserting into table, i can see the value is inserted as 1.234E-5. But i want to insert the value as 0.00001234.
Worst case, if we cannot insert as 0.00001234, is there a way where i can get the 1.234E-5 value from db and get just the last 4 digits from that value and send it back to UI as string? I tried DecimalFormat formatting but not exactly getting what i want.
Also i cannot change the data type as BigDecimal since lot of other code also use this entity.
Thanks.
CodePudding user response:
You can try like this in java
BigDecimal bd = new BigDecimal("1.234E-5");
System.out.println(bd);
It will give you output like this 0.00001234
CodePudding user response:
String.format("%,.8f", d); this will convert your double 1.234E-5 to string "0.00001234".