Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Change dot to comma in expression

  • Change dot to comma in expression

    Posted by Otávio Nascimento on October 7, 2022 at 3:20 pm

    I have a expression that makes a dot every 3 digits: 1.000

    I want to change the dot to comma with a drop down menu.

    If the dropdown menu value is 3 or 4 change to comma: 1,000

    How can I do that with this expression:


    var num = Math.floor(effect(“Number”)(“Valor”).value)

    var num = Comma(num);

    var menu = effect(“Number”)(“Dot and Comma”).value;

    function Comma(number) {

    number = ” + Math.round(number);

    if (number.length > 3) {

    var mod = number.length % 3;

    var output = (mod > 0 ? (number.substring(0,mod)) : ”);

    for (i=0 ; i < Math.floor(number.length / 3); i++) {

    if ((mod == 0) && (i == 0))

    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);

    else

    output+= “.” + number.substring(mod + 3 * i, mod + 3 * i + 3);

    return (output);

    else return number;

    Otávio Nascimento replied 3 years, 7 months ago 3 Members · 9 Replies
  • 9 Replies
  • Dan Ebberts

    October 7, 2022 at 3:44 pm

    I haven’t test this, but it should be close:

    function Comma(number,sep) {
    number = " + Math.round(number);
    if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : ");
    for (i=0 ; i < Math.floor(number.length / 3); i++) {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
    output+= sep + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    else return number;
    }
    var num = Math.floor(effect("Number")("Valor").value);
    var menu = effect("Number")("Dot and Comma").value;
    var mySep = menu == 3 || menu == 4 ? "," : ".";
    num = Comma(num,mySep);
  • Otávio Nascimento

    October 7, 2022 at 5:16 pm

    Error on line 2: Undetermined string constant

  • Dan Ebberts

    October 7, 2022 at 5:28 pm

    Sorry. Line 2 should probably be like this:

    number = '' + Math.round(number);

    There may be others that I didn’t catch from when you posted your original expression without using the </> formatting.

  • Dan Ebberts

    October 7, 2022 at 5:35 pm

    There’s another one in there. I still haven’t been able to completely test this because of your effect names, but I can test everything but those two lines and I think this works now:

    function Comma(number,sep) {
    number = '' + Math.round(number);
    if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(number.length / 3); i++) {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
    output+= sep + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    else return number;
    }
    var num = Math.floor(effect("Number")("Valor").value);
    var menu = effect("Number")("Dot and Comma").value;
    var mySep = menu == 3 || menu == 4 ? "," : ".";
    num = Comma(num,mySep);
  • Otávio Nascimento

    October 7, 2022 at 6:25 pm

    It worked! Thank you for letting me know about this </> format.

    One more thing that I didnt mention I want to add is:

    If the dropdown menu value is 5 change to “_”: 1_000

    If the dropdown menu value is 6 change to ” “: 1 000

    How the expression looks like?

    You can test with this:

    var num = Math.floor(effect("Slider Control")("Slider").value);

    var menu = effect("Dropdown Menu Control")("Menu").value;

    var mySep = menu == 3 || menu == 4 ? "," : ".";

    num = Comma(num,mySep);

  • Dan Ebberts

    October 7, 2022 at 7:22 pm

    I think I’d do it this way:

    function Comma(number,sep) {
    number = '' + Math.round(number);
    if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(number.length / 3); i++) {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
    output+= sep + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    else return number;
    }
    var num = Math.floor(effect("Number")("Valor").value);
    var menu = effect("Number")("Dot and Comma").value;
    switch (menu){
    case 3:
    case 4:
    var mySep = ",";
    break;
    case 5:
    var mySep = "_";
    break;
    case 6:
    var mySep = " ";
    break;
    default:
    mySep = "."
    break;
    }
    num = Comma(num,mySep);
  • Otávio Nascimento

    October 7, 2022 at 8:00 pm

    Thank you!

  • Filip Vandueren

    October 8, 2022 at 1:36 pm

    I would do it compactly like this, letting Javascript do the number parsing/formatting

    myNumber = Math.floor(1234567890.835);

    mySeparator = "_";

    new Intl.NumberFormat('en-US').formatToParts(myNumber).reduce((r,e) => r+(e.value=="," ? mySeparator : e.value), '');

  • Otávio Nascimento

    October 8, 2022 at 4:39 pm

    That’s great! Thank you.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy