Skip to main content
LivingRoom
Participating Frequently
August 8, 2021
Answered

Expression that returns a specific digit in a longer number

  • August 8, 2021
  • 4 replies
  • 2204 views

I would like to split the number returned by a slider into individual digits to animate other things with it.

E.g. reading out the 4th digit of the slider number, 302534 returns the 4th digit = 2 or the 6th digit which is 3. Or in otherwords is there an expression to get the value of a specific digit position in a number that consists of several digits…Thank you

This topic has been closed for replies.
Correct answer Dan Ebberts

Something like this maybe:

 

s = effect("Slider Control")("Slider");
digit = 4;
Math.floor(s/Math.pow(10,digit-1))%10

 

4 replies

Mylenium
Legend
August 9, 2021

Probably as simple as a

 

regEx.match(2,1);

 

with the first number being the digit to return and the second merely saying to only return one character. If you're sure it's just numbers you don't need any other checks, though more regex-savvy people would probably give you a much longer example.

 

Mylenium

LivingRoom
Participating Frequently
August 9, 2021

Thank you Mylenium for your expertise and quick reply.

Community Expert
August 9, 2021

By far the simplest way to set the number of decimal points is to use toFixed().

 

For example:

 

effect("Slider Control")("Slider").value.toFixed(2);

 

will always give 2 decimal places like this: 0.00 or 1.03

 

effect("Slider Control")("Slider").value.toFixed(1);

 

 will always give 1 decimal place like this: 10.2 or 24039.9

 

That's all there is to it. I use toFixed all the time. 

 

Once you set the number of decimal places you can use

sliderValue = effect("Slider Control")("Slider").value.toFixed(3);
asString = String(sliderValue) // .replace(/\D/g,'');
result = + asString[4];
result;

But beware that the number in brackets counts the digits from the left so 234.573 would return 5. but 34.573 would return the 7. That's the only problem with converting to a string. I don't know how to count from the right.

 

The Math.Floor solution counts from the right.

 

LivingRoom
Participating Frequently
August 9, 2021

Thank you Rick for your expertise and quick reply.

zsaaro
Inspiring
August 8, 2021

You can also try converting that number from the slider into a string. You can then access a character of that string by index and convert it back to a number. Something like this:

var sliderValue = effect("Slider Control")("Slider");
var asString = String(sliderValue) // .replace(/\D/g,'');
var result = +asString[2];
result;

In the square brackets you can type the index of the character. The + sign converts it back to a number.

Note that this will include non numbers (if your slider is a decimal like 200.5 the "." will be part of the string, at index 3). You can uncomment the "replace" method and this will clean your string of any character that isn't a number.

 

LivingRoom
Participating Frequently
August 9, 2021

Thank you Saaroz for your expertise and quick reply.

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
August 8, 2021

Something like this maybe:

 

s = effect("Slider Control")("Slider");
digit = 4;
Math.floor(s/Math.pow(10,digit-1))%10

 

LivingRoom
Participating Frequently
August 9, 2021

Thank you Dan for your expertise and quick reply. Your code perfectly solved my problem. I used it to manipulate video footage of a counter on a laboratory display. I cut out the digits from 0-9 and with your expression and time-remap I was able to control the counter of the filmed display. Thank you.