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

substring() not a function?

Participant ,
Aug 25, 2016 Aug 25, 2016

Hi!

trying to do the simple task of separating 6 characters (numbers) and adding a space after the first 3.


My script reads:

siffror = parseFloat(Math.round(thisComp.layer("Räknare Slider").effect("Slider Control")("Slider")));

s1 = siffror.substring(0, 3);

s2 = siffror.substring(3, 6);

"Co2 saved: " + s1 + " " + s2 + " ton";

it says siffror.substring is not a function.

What am I doing wrong here?

TOPICS
Expressions
5.9K
Translate
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

LEGEND , Aug 25, 2016 Aug 25, 2016

siffror = parseFloat

You have things wrong. parseFloat() parses a string to a number, not the other way around. You convert a numeral to text using the .toString() method. For your substring to produce predictable results you will also have to truncate the actual length by rounding your value or setting it to a fixed precision with the .toFixed() method. I would strongly recommend that you read up on this stuff.

Mylenium

Translate
LEGEND ,
Aug 25, 2016 Aug 25, 2016

siffror = parseFloat

You have things wrong. parseFloat() parses a string to a number, not the other way around. You convert a numeral to text using the .toString() method. For your substring to produce predictable results you will also have to truncate the actual length by rounding your value or setting it to a fixed precision with the .toFixed() method. I would strongly recommend that you read up on this stuff.

Mylenium

Translate
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
Participant ,
Aug 25, 2016 Aug 25, 2016
LATEST

Hi Mylenium,

thanks! You are correct, I got everything wrong.

Lucky thing I found a solution,

siffror = Math.floor(thisComp.layer("Räknare Slider").effect("Slider Control")("Slider"));

if (siffror.toString().length < 5) {

"Co2 saved: " + siffror + " ton";

} else {

s1 = siffror.toString().substring(0, 2);

s2 = siffror.toString().substring(2, 5);

"Co2 saved: " + s1 + " " + s2 + " ton";

}

here is my correct code:

Translate
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