0

If I have a double value 333.33333 and I want the result the next value after the point like 334.

If value is 22.2 then result 23

If value is 22.01 then result 23

If value is 22.50 then result 23

Please help

EDIT:

Based on the user's response to a question posted in the comments:

If value is 22 then result 23 (Ceiling does not work here)

Community
  • 1
  • 1
Gitz
  • 810
  • 1
  • 17
  • 48
  • 1
    How `333.33333` should be `3334`? Do you mean `334`? Any effort so far to solve your problem? – Soner Gönül Jul 29 '15 at 06:56
  • What about negative numbers? – vesan Jul 29 '15 at 06:57
  • Do you need the next value of 22 to be 23? – Tyree Jackson Jul 29 '15 at 07:01
  • 1
    yes !! if i get any point value then it should be the next value @TyreeJackson – Gitz Jul 29 '15 at 07:03
  • @Gitz Then try the answer that I posted. – Tyree Jackson Jul 29 '15 at 07:05
  • 3
    This is not quite a duplicate of the question suggested, though the original version was poorly worded. Should be something more like "How do I get the next whole number after a given floating point value in C#?" – Nimrand Jul 29 '15 at 07:22
  • @Gitz With the new title of your question, the answer I posted should work for you. If so, I would appreciate it if you could accept it. Thanks! – Tyree Jackson Jul 29 '15 at 08:01
  • @TyreeJackson Okay i'll let you know after its done – Gitz Jul 29 '15 at 08:02
  • @BenjaminGruenbaum This post is not a duplicate. The author is not trying to round a number. She is trying to get the next whole number after any given number including potentially another whole number. This is slightly different than rounding as rounding would not give the correct answer. – Tyree Jackson Jul 30 '15 at 19:23

1 Answers1

3

Since the author is trying to get the next whole number for any given number as opposed to simply rounding up to the next whole number, we must first obtain the integral portion of the first number and add 1 to it. Simply rounding the first number up will not work since rounding any whole number up is in fact the whole number itself and not the next whole number. To do this we can use Math.Floor to get the whole number (by rounding down) and then add 1.

var nextNumber = Math.Floor(oldNumber) + 1;
Tyree Jackson
  • 2,588
  • 16
  • 22