Copy link to clipboard
Copied
Hello everyone, could someone explain to me why the two digits after the dot are not included in the result of the sum of all values?
How to fix this failure? Thanks.
a = 69.30
b = 217.60
c = 56.00
d = 28.00
tt = parseInt(a)+parseInt(b)+parseInt(c)+parseInt(d);
alert (tt.toFixed(2))
@smithcgl9043167 – Does the following link help? The integer is obtained by simply truncating the input.
http://www.javascripter.net/faq/convert2.htm
// Examples (comments in each line give the conversion results):
parseInt('123.45') // 123
Your variables are already numbers, not strings, so no need to parseInt():
var a = 123;
var b = '123';
alert(typeof(a));
alert(a.toSource());
alert(typeof(b));
alert(b.toSource());
Therefore, wouldn't this give your desired result?
a = 69.30;
b
...
Hello @smithcgl9043167,
As @Stephen_A_Marsh has already pointed out "Your variables are already numbers, not strings, so no need to parseInt()" besides the parseInt(); function has some known bugs when used with ExtendScript.
If ever the case your variables were strings, you could use Number(); to convert the strings; like in the example below.
a = "69.30"
b = "217.60"
c = "56.00"
d = "28.00"
tt = Number(a) + Number(b) + Number(c) + Number(d);
alert (tt.toFixed(2));
Regards,
Mike
Copy link to clipboard
Copied
@smithcgl9043167 – Does the following link help? The integer is obtained by simply truncating the input.
http://www.javascripter.net/faq/convert2.htm
// Examples (comments in each line give the conversion results):
parseInt('123.45') // 123
Your variables are already numbers, not strings, so no need to parseInt():
var a = 123;
var b = '123';
alert(typeof(a));
alert(a.toSource());
alert(typeof(b));
alert(b.toSource());
Therefore, wouldn't this give your desired result?
a = 69.30;
b = 217.60;
c = 56.00;
d = 28.00;
tt = (a)+(b)+(c)+(d);
alert (tt.toFixed(2));
If your values were strings rather than numbers, instead of parseInt() I believe you should be looking for the absolute:
a = '69.30';
b = '217.60';
c = '56.00';
d = '28.00';
tt = Math.abs(a)+Math.abs(b)+Math.abs(c)+Math.abs(d);
alert(tt.toFixed(2));
Copy link to clipboard
Copied
Hello @smithcgl9043167,
As @Stephen_A_Marsh has already pointed out "Your variables are already numbers, not strings, so no need to parseInt()" besides the parseInt(); function has some known bugs when used with ExtendScript.
If ever the case your variables were strings, you could use Number(); to convert the strings; like in the example below.
a = "69.30"
b = "217.60"
c = "56.00"
d = "28.00"
tt = Number(a) + Number(b) + Number(c) + Number(d);
alert (tt.toFixed(2));
Regards,
Mike
Copy link to clipboard
Copied
@Mike Bro wrote:If ever the case your variables were strings, you could use Number(); to convert the strings; like in the example below.
Regards,
Mike
Thanks, Mike, I didn't know about Number() !
It drove me crazy wondering why a prompt() would screw up the calculation of the numbers input into it – until I understood that it returned a string.
Copy link to clipboard
Copied
Hi @Stephen_A_Marsh and @Mike Bro , Thanks for helping me, both solutions work here.
I added parseInt() because in my original script the values of variables even though numbers were concatenating.
Copy link to clipboard
Copied
You could also try something like that:
var a = "69.30";
var b = "217.60";
var c = 56.00;
var d = 28.00;
var tt = a*1+b*1+c+d;
alert (tt.toFixed(2));
Copy link to clipboard
Copied
@pixxxelschubser – I stumbled over this once when making a script, however I didn't ask the question.
Why does multiplying a string create a number?
Copy link to clipboard
Copied
I found the following while working on something else. Not having a formal JS background I obviously didn't know about "type coercion":
https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion
https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/
Copy link to clipboard
Copied
It works. That has been enough for me so far.
😉
Here are 4 variations that should achieve the desired result:
var a = "69.30";
var b = "217.60";
var c = "56.00";
var d = "28.00";
var tt = eval(a) + parseFloat(b) + Number(c) + d*1;
alert (tt.toFixed(2));
A side note:
ParseInt() only works for integers - for floating point numbers you should use ParseFloat()