Skip to main content
ollef84860883
Participant
January 24, 2019
Answered

Decimal numbers in expression

  • January 24, 2019
  • 2 replies
  • 11022 views

Hi! I have created a counter on a text layer that counts length in increments om 40 cm controlled by a slider, I added this expression to the source text property and it works great except for the decimals. 

t = effect("Slider Control")("Slider")

x = Math.round(t/.4)*.4+0.0001

x = x + " Meter"

x.toString().replace(".", ",");

It now counts 0,001 Meter.... 0,4001 Meter... 0,8001 Meter.... 1,2001 Meter..... but i want it to only show 2 decimals: 0,40..0,80..1,20..1,60...

I added the +0.0001 in Math.round to get decimals at the whole numbers. I.e. 4 Meter is now 4,0001 Meter... But how do I remove the last two digits? Or is there another way to always show 2 decimals? I need the counter speed to be controlled over time and masking is a problem since the font I use is not monospaced.

Thank you.

This topic has been closed for replies.
Correct answer Dan Ebberts

Sorry, I completely missed the increments of 0.40 part. That would change it to:

t = effect("Slider Control")("Slider").value;

x = (Math.round(t/.4)*.4).toFixed(2) + " Meter";

x.toString().replace(".", ",");

Dan

2 replies

ollef84860883
Participant
January 24, 2019

Solution:

I added another "replace string" at the bottom and removed the last "01" in the decimals with empty space. I then created a second text layer and pick whipped the source text to layer 1 source text.

full expression, Layer 1 :

t = effect("Slider Control")("Slider")

x = Math.round(t/.4)*.4+0.0001

x = x + " Meter"

x.toString().replace("01", "");

In Layer 2 i added the:

.toString().replace(".", ",");

string and made this the actual visible counter.

Full expression, Layer 2.

thisComp.layer("Layer1").text.sourceText

.toString().replace(".", ",");

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
January 24, 2019

Sorry, I completely missed the increments of 0.40 part. That would change it to:

t = effect("Slider Control")("Slider").value;

x = (Math.round(t/.4)*.4).toFixed(2) + " Meter";

x.toString().replace(".", ",");

Dan

ollef84860883
Participant
January 25, 2019

Thank you!

Dan Ebberts
Community Expert
Community Expert
January 24, 2019

Try it this way:

t = effect("Slider Control")("Slider").value;

x = t.toFixed(2) + " Meter";

x.toString().replace(".", ",");

Dan

ollef84860883
Participant
January 24, 2019

Should i add this somwhere in my expression? If i just use this i only get a "normal counter" I.e. 1,25 Meter...1,26 Meter...1,27 Meter....
I need the counter to count up in increments of + 0.40 meter with two decimal digits. 0,40 Meter... 0,80 Meter....1,20 Meter...1,60 Meter