-3

i need to round a double as it ends with 49 and 99 at the end, fx the number 2138 should be rounded to 2149, and number 2150 should be rounded to 99, which makes it numbers 1-48 goes to 49 and 50-98 goes to 99 everything i found it was about rounding decimal to .99, It has to be double because of the value is already rounded to 0 decimal places with this code:

DecimalFormat decimalFormat = new DecimalFormat("#");
String fromattedDouble = decimalFormat.format(xxx);
Mercy
  • 37
  • 2
  • 9
  • 1
    *2132 should be rounded up to 2149* what ? – Ravi Mar 27 '17 at 06:11
  • What is the exact logic you need? It's unclear from the question. – TDG Mar 27 '17 at 06:14
  • so 2150 should be rounded to 2249? – Nick Ziebert Mar 27 '17 at 06:17
  • Sort for bad explanation, the number 2150 should be rounded to 99, which makes it numbers 1-48 goes to 49 and 50-98 goes to 99 – Mercy Mar 27 '17 at 06:25
  • Your code rounds it and converts it to a *String.* There is no apparent need to convert it back to a double. – user207421 Mar 27 '17 at 06:28
  • 2137 -> 2149 3178 -> 3199 38->49 89-> 99 – Mercy Mar 27 '17 at 06:28
  • Your rounding rules seem unnatural to me. What happens to `49.5`? – Tim Biegeleisen Mar 27 '17 at 06:29
  • 99! I get it now... – Nick Ziebert Mar 27 '17 at 06:29
  • Tim, number 49.5 is converted into string 50 – Mercy Mar 27 '17 at 06:30
  • smacks my head... – Nick Ziebert Mar 27 '17 at 06:31
  • I got double fx 237.7 I round the double to 0 decimal places -> 238 and I need to round that number to 249 – Mercy Mar 27 '17 at 06:32
  • if it is 1-48 round it to 50 and then minus 1. if it is 51-98, round it to 100 then minus 1. – msagala25 Mar 27 '17 at 06:32
  • You really should step back and **clarify** your requirements beyond any doubt. Example: are you even aware of the details of floating point numbers, and their representation within computers --- meaning: do you understand the theory behind that --- see http://stackoverflow.com/questions/588004/is-floating-point-math-broken for starters. I got the impression that you are not at all clear about the things you want to achieve. And I wouldnt call that "rounding"; it is more of "classification" function that maps certain ranges onto a specific result. – GhostCat Mar 27 '17 at 06:33

4 Answers4

1

This should do what you want:

public static int round(int value) {
    return ((value + 50) / 50 * 50) - 1;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 1
    They rarely do. I guess here the complaint could be that "should do what you want" is not really an explanation; and what is left then is a code only answer. – GhostCat Mar 27 '17 at 06:34
  • @GhostCat That's possible. I figured it's a little redundant to explain a simple math equation. – shmosel Mar 27 '17 at 06:35
  • Somebody else seems to disagree ;-| – GhostCat Mar 27 '17 at 06:36
  • I'll thumbs it back up. But the guy also wants 49.5 -> 50 for some reason. – Nick Ziebert Mar 27 '17 at 06:37
  • Nice answer, it closely resembles mine, except that I posted 10 minutes before you, and there is a zero probability that you didn't see my answer before posting this. At your rep level, you should know better. – Tim Biegeleisen Mar 27 '17 at 06:38
  • @Nick Ziebert, there is no case that the number is 49.5 because it's already converted into 0 decimal places, I got solution from Tim Biegeleisen, thanks for help guys :) – Mercy Mar 27 '17 at 06:41
  • @TimBiegeleisen a) I disagree about the close resemblance. b) Your answer was incorrect at the time (returned 50 instead of 49). c) Your answer is *still* incorrect by OP's [updated explanation](http://stackoverflow.com/questions/43039012/round-up-double-to-49-no-decimal-java/43039315?noredirect=1#comment73163291_43039012). – shmosel Mar 27 '17 at 06:41
  • @Mercy "Tim, number 49.5 is converted into string 50" You confusing all of us haha. – Nick Ziebert Mar 27 '17 at 06:42
  • Hi Shmosel, I removed my downvote, there being nothing wrong with your answer. I got attacked this morning by several people. I would say that in general if someone else's answer is out there, and basically right, you should edit/comment it rather than post your own. Just my feelings. – Tim Biegeleisen Mar 27 '17 at 08:11
  • @TimBiegeleisen I only edit posts if it's a clear mistake/typo. I'll usually leave a comment to correct an oversight or suggest an improvement. But there were several fundamental problems with your solution, so rather than try to debug your code, I came up with my own independently (it was not based on yours despite the debatable similarities). As I pointed out, your answer is still incorrect by OP's requirements. – shmosel Mar 27 '17 at 16:05
1

You can use this method:

public static int round(double value) {
    value = Math.round(value);
    return (int)(value-(value%50))+49;
}

Test cases

0       -> 49
25      -> 49
49      -> 49
50      -> 99
75      -> 99
99      -> 99
100     -> 149
2138    -> 2149
2150    -> 2199
48.5    -> 49
49.5    -> 99
50.5    -> 99
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

One trick you could use would be to double your current number, round it to the nearest 100, then divide by 2:

int start = 2132;                 // 2132
start = start*2;                  // 4264
start = (start + 50) / 100 * 100; // (4264 + 50) / 100 * 100 = 4300
start = start / 2;                // 2150
start = start - 1;                // 2149

Using this logic:

2100 -> 2099
2132 -> 2149
2150 -> 2149
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

The following method has the desired effect:

static int round_to_49(int i) {
    return ((i+50)/50*50)-1;
}

Examples:

50: 99
46: 49
145: 149
154: 199
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43