• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Doubt about the sum of values

Engaged ,
Jul 07, 2022 Jul 07, 2022

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))
TOPICS
Actions and scripting

Views

342

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 2 Correct answers

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';
alert(typeof(a));
alert(a.toSource());
alert(typeof(b));
alert(b.toSource());

 

Therefore, wouldn't this give your desired result?

 

a = 69.30;
b
...

Votes

Translate

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

alert (tt.toFixed(2));

 

 

Regards,

Mike

 

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 07, 2022 Jul 07, 2022

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

 

Votes

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

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

 

Votes

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

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.

Votes

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
Engaged ,
Jul 08, 2022 Jul 08, 2022

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.

Votes

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

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

Votes

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

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?

Votes

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

Copy link to clipboard

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/

 

 

Votes

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

Copy link to clipboard

Copied

@Stephen_A_Marsh 

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

 

 

 

@smithcgl9043167 

A side note:

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

Votes

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