jquery - Javascript Math.floor issue between specific range of numbers -
i'm facing issue math.floor function of javascript below scenario:
1) value betwwen 8192 , 10484,
if type 8192.8 -> math.floor converts 8192.79 if type 8192.88 -> math.floor converts 8192.87 if type 8192.3 -> math.floor converts 8192.29
strange part except range given above function works fine.
html: <div data-bind="text: popincrease"></div> <input type="text" data-bind="value: userinput, valueupdate: 'afterkeydown'" /> javascript: var viewmodel = function () { var _self = this; _self.userinput = ko.observable(); _self.popincrease = ko.computed(function () { return parsefloat((math.floor(_self.userinput() * 100) / 100)).tofixed(2); }); }; ko.applybindings(new viewmodel());
jsfiddle:https://jsfiddle.net/91z5bdy4/1/
when changed 100 1000 solved error not understand why happened on first place?
you can switch this:
return parsefloat(_self.userinput()).tofixed(2);
working version of jsfiddle: https://jsfiddle.net/jfriend00/5rll04lk/
or, if want work around of idiosyncrasies of .tofixed()
, can use this:
return (math.round(_self.userinput() * 100) / 100).tofixed(2);
working jsfiddle: https://jsfiddle.net/jfriend00/xx2aj2l0/
this solution passes 3 of test cases.
Comments
Post a Comment