Solved

round up issues.

Posted on 2004-10-06
9
195 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:chainreaction
  • 4
  • 3
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12236343
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
0
 

Author Comment

by:chainreaction
ID: 12245018
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.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 12246466
> 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 them....save 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...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:chainreaction
ID: 12247121
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??
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12248053
Why don't you define it as type "DOUBLE"?
0
 

Author Comment

by:chainreaction
ID: 12255364
in the database? Do we have the DOUBLE data type in databse??
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12256814
yeah.  then you shouldn't need to round the double at all...
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with LDAp login using jsp 6 109
maven project in eclipse 11 57
spring example errors 33 200
spring JDBC Template example error 26 162
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now