round up issues.

Hi i am having some problem.
let say i have this value,

double amount = 1000;

if I divide this 1000 with 12, i will have 833.33333333333
so, what i do is i round it with 2 decimal point, to become 833.33

but when end of the day, i want to sum up all the 833.33, means 833.33 * 12, i will only get 9999.96, lost 0.04

How can i resovle this issues?? Can i use BigDecimal?? If yes, how can i apply the technique to sovle this issues?
Who is Participating?
TimYatesConnect With a Mentor Commented:
> i onluy cater for 4 decimal.


Why not assign the field type as DOUBLE, then it is a 64 bit floating point number, and you can just store the java double as it is into the databse...

> How many decimla point "double" and bigDecimal can handle?

It depends on the size of the integral part of the double, but it should be plenty for your application...

>  but how many decimla point i should keep?

All of it in a double, never change it from a double and just display it to the user as a String...  But really it will be a double...
Don't round it to 2dp...

Just display it as 2dp to the user, using java.text.DecimalFormat, but keep the number as 833.3333333333333333
chainreactionAuthor Commented:
but how many decimla point i should keep?

How many decimla point "double" and bigDecimal can handle?

Fiurther, this 8333.3333 is going to stored into the DB and in the data field, i onluy cater for 4 decimal.

What is the common solution for this?

Thank You.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

chainreactionAuthor Commented:
OK. I am using Oracle, let say for example. in the table filed. if i choose the table field  type as Number, i will have to specified the "scale" of the Number. I have to decide what is the decimal point i should allow it to insert.

And if i am in Db2, i also need to specify the scale of the number data type.

Are we referring to the same things??
Why don't you define it as type "DOUBLE"?
chainreactionAuthor Commented:
in the database? Do we have the DOUBLE data type in databse??
yeah.  then you shouldn't need to round the double at all...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.