I defined two doubles:
double abc1 = 0.0001;
double abc2 = 0.0001;
now if I print them:
println "Abc1 "+abc1;
println "Abc2 "+abc2;
it returns:
Abc1 1.0E-4
Abc2 1.0E-4
While if I add them:
println "Abc3 "+abc1+abc2;
It returns:
Abc3 1.0E-41.0E-4
rather than:
Abc3 2.0E-4
Why does this happen?
This is because addition operator works from left to right and you start with string, hence the addition operator works as concatenation operator in your case.
This:
println "Abc3 "+abc1+abc2;
will be done step by step like this:
println "Abc3 "+abc1+abc2;println "Abc3 1.0E-4"+abc2;println "Abc3 1.0E-41.0E-4";If you want to get the result you are expecting, do it like this:
println "Abc3 " + (abc1 + abc2);
Here you are prioritizing the addition before the concatination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With