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

Appending a "%" to numbers from a string

New Here ,
Oct 06, 2011 Oct 06, 2011

Copy link to clipboard

Copied

I have an expression that is working in animating a percentage based on a slider control.

n=thisComp.layer("Null 1").effect("Pie Slice 2")("Slider")

nRound=Math.round(n*10)/10;

check = nRound % 1

if (check == 0){

""  + nRound  + ".0" + "%"

}else{

nRound

}

What I can't figure out is why the "%" only appears when the number is whole and not there constantly? After reading through Dan Ebbert page, he indicates that you can in fact add it (which I thought I did correctly) but only on the whole numbers. When it's animating through the "tenths" the % does not appear.

I'm sure it's something simple, but I can't figure it out.

Thanks for your help.

TOPICS
Expressions

Views

1.3K

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

Enthusiast , Oct 07, 2011 Oct 07, 2011

In your if/else statement you are only adding the % in the cases where it is a whole number. You would need to change it to:

if (check == 0){

""  + nRound  + ".0" + "%";

} else {

""  + nRound + "%";

}

Also, here's an alternative way to write the expression:

n=thisComp.layer("Null 1").effect("Pie Slice 2")("Slider");

n.value.toFixed(1) + "%";

Votes

Translate

Translate
Enthusiast ,
Oct 07, 2011 Oct 07, 2011

Copy link to clipboard

Copied

In your if/else statement you are only adding the % in the cases where it is a whole number. You would need to change it to:

if (check == 0){

""  + nRound  + ".0" + "%";

} else {

""  + nRound + "%";

}

Also, here's an alternative way to write the expression:

n=thisComp.layer("Null 1").effect("Pie Slice 2")("Slider");

n.value.toFixed(1) + "%";

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
New Here ,
Oct 07, 2011 Oct 07, 2011

Copy link to clipboard

Copied

Awesome, thanks so much.

I also found out that if you take the "check" out all together and write it like this:

n=thisComp.layer("Null 1").effect("Pie Slice 2")("Slider")

nRound=Math.round(n*10)/10;

""  + nRound  + ".0" + "%"

Several ways it seems. Actually, now I'm curious. Does the check need to be in there at all? What is the logic behind it and the way you wrote it as well?

Thanks again!

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
Enthusiast ,
Oct 07, 2011 Oct 07, 2011

Copy link to clipboard

Copied

LATEST

It looks like you were rounding it down to one decimal place, then checking if it was a whole number, and if so you were needing to add ".0" to the end as it wouldn't be there otherwise.

The two line alternative expression I gave was complete and didn't need the check. I bypassed all that by using .toFixed(1) which sets any numbers to one decimal place.

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