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

100

Likes

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

Adobe Community Professional , 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...

Likes

Translate

Translate
Advocate , 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

Likes

Translate

Translate
Adobe Community Professional ,
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));

 

Likes

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
Advocate ,
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

 

Likes

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

Copy link to clipboard

Copied


@mikeb41294032 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.

Likes

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 @mikeb41294032 , 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.

Likes

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
Adobe Community Professional ,
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));

Likes

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
Adobe Community Professional ,
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?

Likes

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

Copy link to clipboard

Copied

LATEST

@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()

Likes

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