How to take the nth digit of a number in Maxscript

How to take the nth digit of a number in Maxscript

CoxIII_Charles
Observer Observer
668 Views
2 Replies
Message 1 of 3

How to take the nth digit of a number in Maxscript

CoxIII_Charles
Observer
Observer

Hello,

I am trying to get specific values from an Integer in Maxscript (ex. 4321, I want to get the 2nd digit which is 2). This fairly simple to do in Python.  You can either use division and remainder methods:

n=2
value = 4321
SecondDigit = value // 10**n % 10


Or you can convert it to a string and get the character/digit:

value = 4321
NumberString = str(value)
int(NumberString[1]) 

 

I am having a really hard time doing this in Maxscript.  Maxscript is a bit different from python and converting this logic over is difficult. would someone be able to point me in the direction? 

0 Likes
Accepted solutions (2)
669 Views
2 Replies
Replies (2)
Message 2 of 3

miauuuu
Collaborator
Collaborator
Accepted solution

The count is from left to right, so the second number is 2, not 3.

 

 

(
	numberVal = 4231
	((numberVal as string)[2]) as integer
)

 

To get the second from right to left:

 

(
	numberVal = 4231
	numberStr = numberVal as string
	
	numberStr[numberStr.count - 1] as integer
)

 

 

 

 

 

 

https://miauu-maxscript.com/
Message 3 of 3

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

@CoxIII_Charles wrote:

Hello,

I am trying to get specific values from an Integer in Maxscript (ex. 4321, I want to get the 2nd digit which is 2). This fairly simple to do in Python.  You can either use division and remainder methods:

 

n=2
value = 4321
SecondDigit = value // 10**n % 10

 

 


you can do almost the same thing in maxscript:

 

int (mod (123456/(10 ^ n)) 10)