• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Expression that returns a specific digit in a longer number

Community Beginner ,
Aug 08, 2021 Aug 08, 2021

Copy link to clipboard

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

1.2K

Translate

Translate

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 guidelines

correct answers 1 Correct answer

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

 

Votes

Translate

Translate
Community Expert ,
Aug 08, 2021 Aug 08, 2021

Copy link to clipboard

Copied

Something like this maybe:

 

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

 

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

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.

 

Votes

Translate

Translate

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 guidelines
Contributor ,
Aug 08, 2021 Aug 08, 2021

Copy link to clipboard

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.

 

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

Thank you Saaroz for your expertise and quick reply.

Votes

Translate

Translate

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 guidelines
Community Expert ,
Aug 08, 2021 Aug 08, 2021

Copy link to clipboard

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.

 

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

Thank you Rick for your expertise and quick reply.

Votes

Translate

Translate

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 guidelines
LEGEND ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

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

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

LATEST

Thank you Mylenium for your expertise and quick reply.

Votes

Translate

Translate

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 guidelines