Numbers with 2 decimal digits (Javascript)

Contributor ,
Nov 10, 2020 Nov 10, 2020

Copy link to clipboard

Copied

...

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 :

Division_01.png

But sometimes this is what happen :

Division_02.png

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 !...

😉

...

Views

91

Likes

translate

Translate

Translate

Report

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

Contributor ,
Nov 10, 2020 Nov 10, 2020

Copy link to clipboard

Copied

...

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 !...

...

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Nov 10, 2020 Nov 10, 2020

Copy link to clipboard

Copied

Likes

translate

Translate

Translate

Report

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
Contributor ,
Nov 10, 2020 Nov 10, 2020

Copy link to clipboard

Copied

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

Merci Lily !...

Likes

translate

Translate

Translate

Report

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