Seperate integers from 1 variable into different variables

Issue

Not sure how to explain this the best but I have a variable with 4 int’s in it. Is there a simple way to extract the 4 int’s into 4 seperate var’s?

Example:
The variable contains: 4567

And then the output is:

   var1 = 4 
   var2 = 5
   var3 = 6
   var4 = 7

Solution

You can do this:

val input = 4567

val var1 = input / 1000
val var2 = (input % 1000) / 100
val var3 = (input % 100) / 10
val var4 = (input % 10)

Answered By – Ivo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published