How to identify the end digit of a number is less than 5

Hi all

Please can I ask if anybody can help me write the code to see if the end digit is less than 5 and then round this down to the nearest 5, regardless.

e.g, if the number given was 62 this would be changed to 60; if this was 67 it would become 65

In the xmal I am looking at, in an IF statement there is this

cint(in_Integer.ToString.Substring(in_Integer.ToString.Length-1)) < 5

in the THEN there is cint(in_Integer) -cint(in_Integer.ToString.Substring(in_Integer.ToString.Length-1))
in the ELSE there is cint(in_Integer) -cint(in_Integer.ToString.Substring(in_Integer.ToString.Length-1)) + 5

I’m struggling to follow these; is there a ‘simpler’ way to write the code or please can someone explain what is happening

Thanks in advance
Jordan

@jordrowley Is the logic working? :sweat_smile:

Hey, yeah works fine, which is good, but don’t quite follow the code

Generic - Round Value Down To Nearest 5.xaml (14.8 KB)

I am not sure about your variable types, but I set up two variables as Decimal types, and have an assign activity like this:

variable2 = math.Round(variable1/5)*5

You can technically change both 5’s into any number and it should round to that number.
Again, be careful with the variable types here, and test it with your data.

EDIT:

Just saw that you want to round down for every value. Logic is still the same, but instead of math.round, use math.floor. You can also use math.Ceiling to round up.

variable2 = math.floor(variable1/5)*5
3 Likes

Answer of someone who knows maths :+1:

1 Like

wow :astonished:

That works and is so, so much simpler that code I’d provided too! Thank you, so much :smiley:
Jordan

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.