开发者

how to change amount type to number type to perform arithmetic operations in javascript

In javascript val is of amount type like $1,056.89. Now i want val to be performed with arithmetic operations but alert gives me as NaN instead of arithmetic value. if i take off Number then y value is as 1,056.8910 .how can i make val to numeric type and perform arithmetic operations?

var val= document.getEleme开发者_Python百科ntById('<%= DataItemValue4.ClientID%>').firstChild.nodeValue;
            val= val.replace(/^[\s$]+|[\s]+$/g, '');
            y = Number(val)+ 10;
            alert(y);

can any one help me out with this !

thanking you, michaeld


Remove all non numeric characters and coerce the string to a number:

val = + val.replace(/[^\d.-]/g, "");


After you strip out any non-numeric characters, try using parseFloat to convert the value to a floating point number.

val = val.replace(/[^0-9.]/g, '');
y = parseFloat(val);


The code below should get things working -

var val= '$1,056.89';
val= val.replace(/^[\s$]+|[\s]+$|,/g, '');
y = parseFloat(val)+ 10;
alert(y);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜