6

How can I round a value UP to the next multiple of 10 in VB ?

e.g.

19 -> 20
35 -> 40
21 -> 30

I found so things saying use a round function but when i type it in to my IDE (Microsoft Visual Studio Express 2015) it doesn't recognise it.

Thanks in advance

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
Huddlespith
  • 123
  • 1
  • 2
  • 8
  • 1
    Googling `round up to next multiple of 10 in visual basic` seems to yield plenty of results, e.g. http://stackoverflow.com/questions/326476/how-to-round-a-number-in-vba-to-the-nearest-5-or-10-or-x. There seem to be more ways than just `round()` – Pekka Dec 19 '15 at 20:12
  • i've tried this but it doesn't recognise round `round(X/N)*N` – Huddlespith Dec 19 '15 at 20:15
  • thanks @CodeiSir that worked perfectly – Huddlespith Dec 19 '15 at 20:18

2 Answers2

8

To round up use the ceiling function. (info here)

myNumber = Math.Ceiling(myNumber / 10) * 10

Deviding it first by 10 and then multiplying it again with 10 will do the trick.

Update: in case you are wondering, there are no problems with Integer

CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • This isn't a great solution. The only two overloads of `Math.Ceiling` available are for either `Double` or `Decimal`. In the case of `Integer` the call to `Math.Ceiling` gets cast to `Double`. This could lead to rounding errors. – Enigmativity Dec 20 '15 at 01:33
  • Downvoting means "This answer is not usefull", why did you downvote? Of cause using function has the effects of the funtions, and for this its more then OK to use it. – CoderPi Dec 20 '15 at 08:30
  • Downvoting is used for answers that "are least useful", and not "not useful". In your answer you're doing some type casting which only works with `Option Strict Off`. If you set `Option Strict On` then this code won't even compile. This is not a good robust solution. – Enigmativity Dec 20 '15 at 13:46
  • You are saying nobody should ever use `Math.Ceiling`, but thats just your opinion. The answer is in no way wrong. He wanted to use `round` anyway, so thats exactly what he was looking for. – CoderPi Dec 20 '15 at 16:26
  • By the way, a 32bit Integer is perfectly represented by double. The function is perfectly suited for Integer. In case you are wondering, it also has a Decimal overload. – CoderPi Dec 20 '15 at 16:31
  • 1
    @Enigmativity `Math.Ceiling(CDec(myNumber) / 10) * 10` compiles fine, even with `Option Sctrict On` – Jeroen Dec 20 '15 at 21:24
  • @Jeroen - Not when you're assigning it back to `myNumber`, as in `myNumber = Math.Ceiling(myNumber / 10) * 10`. – Enigmativity Dec 20 '15 at 23:10
  • @CodeiSir - I'm saying that you shouldn't use `Math.Ceiling` for any other types other than `Double` or `Decimal`. Spotting bugs based on implicit casting is hard. Someone will come, read this answer, adapt it to a different situation and get a bug. – Enigmativity Dec 20 '15 at 23:15
  • @Enigmativity Pedantic much? :-) Try `thisNum = (Math.Ceiling(CDec(thisNum) / 10) * 10).ToString` Tadaa! Problem solved – Jeroen Dec 20 '15 at 23:15
  • @CodeiSir - The fact that he wanted to use `Round` shows that he doesn't understand about the issues with implicit casting. Suggesting `Ceiling` just compounds the confusion. – Enigmativity Dec 20 '15 at 23:16
  • @Jeroen - That doesn't compile either. The type of `myNumber` (or `thisNum` as you put it) is `Integer`. The issue with this not compiling with `Option Strict On` is that the compiler is telling you it is bad to do. Sure you can explicitly cast, but that's just throwing caution to the wind. – Enigmativity Dec 20 '15 at 23:19
  • thisNum is a String :-) – Jeroen Dec 20 '15 at 23:22
  • @Jeroen - That's fine, but the code that I said didn't compile it was `Integer`. – Enigmativity Dec 20 '15 at 23:40
  • @Enigmativity the fact that he wanted to use `round`, shows that he was very well aware of the effects of implicit casting And even if he had Integer first and then Double (wich don't know at all) he would be warned when using strict. – CoderPi Dec 21 '15 at 08:14
  • @CodeiSir - I don't think so. Strict is off by default. As I already said I think that implication of him asking for `Round` is the other way. – Enigmativity Dec 21 '15 at 10:54
  • @Enigmativity so if it's not a problem when strict is on, what's the problem if its off ? – CoderPi Dec 21 '15 at 11:30
  • @CodeiSir - When strict is on your code won't compile - so it **is** a problem. Strict is off by default to handle the "loose" behaviour that VB6 brought to VB.NET. Turning strict on brings VB.NET up to the same rigorous type-safety as C#. Type-safety in a strongly-typed language is usually a pretty good thing. – Enigmativity Dec 21 '15 at 12:47
  • Following your logic, you should go find any post using any implicit typecasting and down vote it. *ridiculous* – CoderPi Dec 21 '15 at 12:58
-2

This is an Integer-based solution.

myNumber = If(myNumber Mod 10 = 0, myNumber, If(myNumber > 0, 10, 0) + 10 * (myNumber \ 10))

It doesn't suffer from rounding and is also is at least 2x faster than using Math.Ceiling.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Math.Ceiling doesn't suffer precision on Integers. You can represent a up to 53bit Integer in double without loss, however VB only uses a 32bit Integer. Thats why Integer can be converted to Double anytime. (https://msdn.microsoft.com/en-us/library/06bkb8w2.aspx?f=255&MSPPError=-2147217396) Also good luck with `mod` on doubles. – CoderPi Dec 20 '15 at 16:34
  • @CodeiSir - Where is the `mod` on doubles? – Enigmativity Dec 20 '15 at 23:20
  • @Enigmativity the OP's question doesn't explicitly define the inputs as `integer`, you interpreted it as such. I thought the "thisNum is a string" would be enough of a hint to that fact :-) If I ever need to do this with an `integer` though, your method gets my vote! – Jeroen Dec 21 '15 at 03:12
  • @Jeroen - Where do you get the "thisNum is a string" from? It's not in the OP's question or any comments. – Enigmativity Dec 21 '15 at 03:25
  • @Enigmativity I made that remark as a joke, you even responded to it. – Jeroen Dec 21 '15 at 04:07
  • @Jeroen - Sorry, I don't get the joke. – Enigmativity Dec 21 '15 at 04:11
  • @Enigmativity you are forgiven :-) – Jeroen Dec 21 '15 at 04:15