javascript - How to assign a variable type to variables in an object -


this question has answer here:

here code

var m = new object(); m.p1 = 37.7; m.p2 = 37.7; ...  function addsubtract(pn){ switch (pn) {     case 1:         var amt = prompt("enter value");         if (amt != null)         {         m.p1 += amt;         }     break;     } } 

basically when enter 1, value of m.p1 becomes 37.71 instead of 38.7

then enter 1.0 , shows 37.711.0. figured out doing concatenation rather addition. tried find way declare type can't figure out how use in variable in object.

i more of c++ person , there obvious i'm missing here. can't find on google.

so have assign type or there way force arithmetic addition?

the result of prompt() string. if add number string, number converted string , concatenation performed.

to convert string float use 1 of following:

var amt = parsefloat(prompt("enter value")); 

...or:

var amt = +prompt("enter value"); 

...or:

var amt = number(prompt("enter value")); 

note when using parsefloat() non-numeric characters @ end ignored, when using unary + method or number() nan if string has characters:

> parsefloat('1.0foo') 1 > +'1.0foo' nan > +'1.0' 1 

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 -