So I need to change doubles such as 143.203030 to 143.20 rounding the second digit up or down(like money) my plan was to turn the double into an array and then if the third number after the decimal is 5 or greater the previous element would be one greater than it was. How do i access each int of a double and how would i turn my array of int back into doubles. Now... This is obviously a terrible and inefficent way, what would be a better idea. I would take the time to code that but I figured explaining it and asking for a better way is the same thing. Well actually i couldn't code that because I dont know to access each part of a double.
Asked
Active
Viewed 2,245 times
0
-
1First, don't store money with a `double` (there are values that can't be stored (accurately) with a `double`). – Elliott Frisch Feb 05 '14 at 20:51
-
1In general it is not posdible to do this, because floating point numbers are stored in base 2 rather than base 10, and have a fixed number of significant bits. – Raedwald Feb 05 '14 at 20:57
-
http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – JNL Feb 05 '14 at 20:59
-
See http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary – Raedwald Feb 05 '14 at 21:00
3 Answers
2
A better idea, especially if you are working with money, is to use the BigDecimal
class. It supports a variety of rounding modes meeting most common financial and engineering requirements.
To meet your immediate needs, you can use BigDecimal
as follows (although it may be best to eliminate the double
type from your program and use BigDecimal
throughout):
String[] round(double[] arr) {
String[] r = new String[arr.length];
for (int idx = 0; idx < arr.length; ++idx) {
r[idx] = BigDecimal.valueOf(arr[idx]).setScale(2, RoundingMode.HALF_UP).toString();
}
}

erickson
- 265,237
- 58
- 395
- 493
2
Try this:
public static roundHundredth(double d) {
return (double)((int)(d*100)+.5)/100;
}
-
@ajb I see. I initially had not done the casting, but then implemented that to account for the required rounding. – Feb 05 '14 at 20:57
-
This is 3 decimal places and I'm not quite sure how this works would you mind explaining with an example. It seems like this way is best(for a beginner java programmer)Not trying to get into other classes on this particular assignment besides math. – user3026468 Feb 05 '14 at 21:26
-
@user3026468 What do you mean "this is 3 decimal places"? Using your original example: `143.203030 * 100` is `14320.3030`. When you cast it to `(int)`, it chops off all the decimal places and becomes `14320`. When you divide by 100, it becomes `143.20`. The `.5` is there to make sure that things like `143.209090` round up. – ajb Feb 05 '14 at 21:30
0
I agree that you shouldn't use a double
to store a money value (although will probably work OK for smaller amounts). The general approach for doing this kind of rounding is
double rounded = Math.round(d * 100) / 100.0;

ajb
- 31,309
- 3
- 58
- 84