error javascript code calculation -


pls consider calculation in javascript code below. example values:

  • investmentfix = 200.00
  • investmentvar = 123.12
  • nrofparts = 5.00

...the console shows strange result value 0200.00615.6. don't understand. noticed values 200.00 , 615.6, - latter result of investmentvar * nrofparts- in result value. i'd expect (and target get) result 815.6 (200+(123.12*5.00).

what goes wrong? relate kind of format issue?

javascript code:

var result =0;  result += (investmentfix + (investmentvar * nrofparts));  console.log(result); 

try following:

result = parsefloat(0,10); result+=parsefloat(investmendfix,10)+parsefloat(investmentvar*nrofparts,10); console.log(result); 

this ensures javascript engine parses variables @ numbers instead of strings.

parsefloat() syntax: parsefloat(mynumber,base);


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -