lundi 14 mars 2016

How to use BigDecimals without using doubles

I'm trying to make this program where you got some different items you can buy.

im storing my prices in a sqlite database (have not decided on datatype other than its going to be string or integer).

i have read for 2 hours now trying to get a hang on this, i understand why i cant use doubles or float values.

but i cant decide: should i store my prices as integers? or Strings.

the problem i see here is that i have ALWAYS been told not to store integers as strings.. and now people tell me it smarts because this is the only way i can keep the accuracy of my prices ?

if i store them as integers i will have a problem of:

int 5 / 100 //i want that to be 0.05
int 50 / 100 //i want that to be 0.50
int 500 / 100 //i want that to be 5.00

how do i do this without using doubles ??

or maybe there is a way to do it with BigDecimals?

BigDecimal bddd1 = bdd1.setScale(2,RoundingMode.HALF_EVEN); // gives me 5.00 instead of 0.05

so how will i do this correctly, always getting 2 decimals.

Aucun commentaire:

Enregistrer un commentaire