Skip to main content
Dirlo
Inspiring
November 10, 2020
Answered

Numbers with 2 decimal digits (Javascript)

  • November 10, 2020
  • 1 reply
  • 825 views

...

Hello Captivaters !...

I have a Javascript issue... I'm working on the division with decimal numbers...

So far my script 1 is :

window.cpAPIInterface.setVariableValue("VD_1", Math.floor(Math.random()*800+200)/100);
var V_1 = VD_1.toLocaleString("fr-FR");
window.cpAPIInterface.setVariableValue("V_2", Math.floor(Math.random()*8)+2);

 

Then I have Expression => VD_R = VD_1 * V_2

 

And my sript 2 is :

var V_R = VD_R.toLocaleString("fr-FR");

 

And eveything seems to be ok :

But sometimes this is what happen :

So I need the V_R variable to display 2 decimal digits !...

I have tried :

V_R.toFixed(2)

After the first line of my script 2... But it doesn't seem to resolve this...

 

So your help is required !...

😉

...

    This topic has been closed for replies.

    1 reply

    Dirlo
    DirloAuthor
    Inspiring
    November 10, 2020

    ...

    I answer myself as I believe I found a solution...

    In fact it appears that the method "toFix" is not compatible with the function "toLocaleString" !!!...

    So I had to put this :

    var V_R = VD_R.toLocaleString("fr-FR", {minimumFractionDigits: 2});

    And it seems to be ok !...

    ...

    Dirlo
    DirloAuthor
    Inspiring
    November 10, 2020

    "C'est l'intention qui compte !..."

    Merci Lily !...