-3

I am using javascript function to execute my string value, I have input like,

"0110 + 0123" = 155 ( I am getting )

But actually output was "233"

var total = "0110 + 0123";
total = ( new Function( 'return ' +  memberData) )();
console.log(total);

How to hide or remove zero while calculating value.

Vijaykarthik
  • 333
  • 4
  • 10
  • 25

2 Answers2

0

Beginning a number with 0 makes it an octal number. Your result is 0233 octal which is 155 decimal.

Test: Type 233 in the JavaScript console of your favorite browser. It will answer 233. Type 0233 and it will answer 155.

Donat
  • 4,157
  • 3
  • 11
  • 26
  • The result is actually `155` - it's never interpreted as octal. The two operands would be immediately translated to decimal - `0110` -> `72` and `0123` -> `83`, so you get the result of `72 + 83`. Or to be more precise, the literal will be translated to the IEEE 754 numeric underneath. Point is, the calculation is not done in octal. – VLAZ May 20 '19 at 11:27
  • yes, the calculation will not be done in octal nor will it be done in decimal. It just does not matter in which base the calculation will be done. It is only about converting the external representation to internal data type and vice versa. This is not the crucial point in my answer. I just did not say in which representation the calculation has been done. – Donat May 20 '19 at 11:50
  • Yes i agree @Donat, then how to archive this calculation. My input will be in string "0110 + 0123 - 0122 * 0122" it may like this. i am passing this string to evaluate output for above string.... – Vijaykarthik May 20 '19 at 17:39
0
var ac = parseInt("0110", 10);
var ad = parseInt("0123", 10);
var total = ac + ad;
console.log(total);
  • Hi @Abhay singh my input will in string "0110 + 0123 - 0122 * 0122" it may like this. i am passing this string to evaluate output for above string.... – Vijaykarthik May 20 '19 at 17:29