Round Up Integer

(389 * 10) / 100

I want the value 0.39 out of the above expression.  But it is giving 0.38.  Whats the best way to apply round up.
fix2009Asked:
Who is Participating?
 
Kevin CrossChief Technology OfficerCommented:
This will result in 38.9.  To get value as 39.0 you can use Math.round to get rounding based on decimal value or Math.ceil to always get next integer up.
double d = (double)(384 * 10) / 100;
		System.out.println(d);
		System.out.println(Math.round(d));
		System.out.println(Math.ceil(d));

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Because the values are integers, it is doing Integer math.  Try like this:

double d = (double) (389 * 10) / 100
0
 
a_bCommented:
Are you sure the expresssion is corecct -

(389*10) = 3890
3890/100 = 38.9
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Kevin CrossChief Technology OfficerCommented:
a_b's point is correct.  My solution is for getting the answer from 38 to 39.  If you are truly getting 0.38 then post the correct formula.
0
 
Kevin CrossChief Technology OfficerCommented:
fix2009,

Glad that helped you.  If you don't mind me asking, why the "B" grade there.  If you need more explanation on something, just ask.  A "B" signifies you have to do research to get to the complete solution; therefore, please advise what is missing so can better help others in the future.

Happy coding!

Regards,
Kevin
0
 
fix2009Author Commented:
Hi mwvisa1,  I think I didnt think before choosing the grade.  Your answer is accurate. I wish I can change it now to 'A'.
0
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.