• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Doubt about the sum of values

# Doubt about the sum of values

Engaged ,
Jul 07, 2022 Jul 07, 2022

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);

TOPICS
Actions and scripting

Views

343

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 Expert , Jul 07, 2022 Jul 07, 2022

@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';

Therefore, wouldn't this give your desired result?

``````a = 69.30;
b``````
...

Advisor , Jul 07, 2022 Jul 07, 2022

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);

Regards,

Mike

8 Replies 8
Community Expert ,
Jul 07, 2022 Jul 07, 2022

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';

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);

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);

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 07, 2022 Jul 07, 2022

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);

Regards,

Mike

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 Expert ,
Jul 07, 2022 Jul 07, 2022

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.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jul 08, 2022 Jul 08, 2022

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.

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 Expert ,
Jul 09, 2022 Jul 09, 2022

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;

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 Expert ,
Jul 09, 2022 Jul 09, 2022

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?

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 Expert ,
Sep 11, 2022 Sep 11, 2022

Copied

LATEST

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/

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 Expert ,
Jul 10, 2022 Jul 10, 2022

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;

A side note:

ParseInt() only works for integers - for floating point numbers you should use ParseFloat()