how to read value zero using parseInt function
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
var resstr=result.toString();
var res=resstr.split(".");
var test=parseInt(res[1].charAt(0));
var test1=parseInt(res[1].charAt(1));
this is my code when my value in res variable is 5.90 then I开发者_Python百科 alert test & test1 variable in test alert it shows correct value i.e. "9" but in test1 alert it shows message like "Nan" if res variable contain value 5.35 then it work correct i.e.test=3 & test1=5 only it does not work when test1 contains value "0" it gives message "Nan"
The problem is that you create a string such as '12.3'
, and split it to 3
. .charAt(1)
on that string returns an empty string, ''
, which parseInt turns into a NaN
.
Well, an easy and hacky fix would be:
test1 = test1 || 0;
You may also consider a calculation instead of string manipulation:
var result = 98.1234;
var d1 = Math.floor(result * 10) % 10;
var d2 = Math.floor(result * 100) % 10;
精彩评论