Finding a single digit
-
I’m back, just noodling around and not committing to anything big :). Does anyone know if there’s a command to determine a single digit in a large number? e.g, the third number in 4567 would be 6. Also, for a single letter in a word? I’ve looked through all the help section in Fuze and I can’t find anything.
-
If you have a string, you can access a character in that string as an array (so using
stringValue[index]
notation, where index is zero based).
You can also cast a number to a string, and then get a digit from that string with the same syntax.
The following code snippet shows how you could apply this:function getDigit(number, digitIndex) // digitIndex is 0 based, so to get the third digit, it needs to be 2 var numberAsString = str(number) // cast the number to a string, so you can access individual characters var digitAsString = numberAsString[digitIndex] // this is how you get a single letter in a word (to answer part of your question), in this case the word is a number as a string var digit = int(digitAsString) // it's possible to cast the digit back to a number (in case you want to do calculations with it) return digit // returning the digit as an int var value = getDigit(4567, 2) // returns 6, since that's the digit at zero based position 2 print(value) // prints 6 update() sleep(10)
-
Amazing, thanks a lot!