• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
• Re: Expression that returns a specific digit in a ...

# Expression that returns a specific digit in a longer number

Community Beginner ,
Aug 08, 2021 Aug 08, 2021

Copied

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

TOPICS
Expressions

Views

382

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Expert , Aug 08, 2021 Aug 08, 2021

Something like this maybe:

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

Likes

8 Replies 8
Community Expert ,
Aug 08, 2021 Aug 08, 2021

Copied

Something like this maybe:

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

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copied

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.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Contributor ,
Aug 08, 2021 Aug 08, 2021

Copied

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.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copied

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Aug 08, 2021 Aug 08, 2021

Copied

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.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copied

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
LEGEND ,
Aug 09, 2021 Aug 09, 2021

Copied

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

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copied

LATEST