Table of Contents

Different ways to format numbers with JS

Ana Updated by Ana

To see this workaround for the WhatsApp channel, review thisΒ article.
Transform 1000 to 1,000

  1. First, we ask for a number and store it in the @number variable

  1. With a Code block we add the function that will transform the number:

function fixValue(value){
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");}

this.setCustomData({'numberwithcomma': fixValue(ΒΏΒ·@numberΒ·?)})

  1. As we created the value with Javascript, we need to add an input from the user so the value is available in Landbot scope:

  1. In the send a message we can already display the new value with the new variable.

Get two decimals .00
  1. In the Question Number, we will save it with the @number variable:

  1. With a Code block we add the function that will transform the number:
this.setCustomData({numbertwodecimals:parseFloat(@number).toFixed(2)})

  1. As we created the value with Javascript, we need to add an input from the user so the value is available in Landbot scope:

  1. In the send a message we can already display the new value with the new variable:

Transform 1000 to 1,000.00

  1. First, we ask for a number and store it in the @number variable
  1. With a Code Set block we add the function that will transform the number:

const num1 = @number;

function formatNumber(num1) {
const decimalNumber = num1.toFixed(2);
const [integerPart, decimalPart] = decimalNumber.split('.');
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return `${formattedInteger}.${decimalPart}`;
}

const formattedNumber = formatNumber(num1);
return formattedNumber;

  1. In the send a message we can already display the new value with the new variable.

These scripts and how to's are not native functionalities. Landbot won't be able to support, help or guarantee these scripts and how to's. These Workarounds and How to's are for developers, as a learning and example material on how to extend or modify some of the platform limitations.Β  Due to platform updates, some scripts might stop working in the future.Please, note that in case of Scripts and Workaround the Custom Success Team can deliver limited support.

How did we do?

Two-Step Email Verification

Trigger Event if User Abandons Chat

Contact