Copy link to clipboard
Copied
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?
1 Correct answer
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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:

