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

Decimal numbers in expression

New Here ,
Jan 24, 2019 Jan 24, 2019

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.

TOPICS
Expressions
10.8K
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

Community Expert , Jan 24, 2019 Jan 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

Translate
Community Expert ,
Jan 24, 2019 Jan 24, 2019

Try it this way:

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

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

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

Dan

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
New Here ,
Jan 24, 2019 Jan 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

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
New Here ,
Jan 24, 2019 Jan 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(".", ",");

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
Community Expert ,
Jan 24, 2019 Jan 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

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
New Here ,
Jan 25, 2019 Jan 25, 2019
LATEST

Thank you!

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