Wednesday, November 6, 2013

May I cast my string to an integer in JavaScript?

One way to find out is like so...

if (!isNaN(parseInt(myThingInStringFormat))) {
   var useMeDownstream = parseInt(ahNewMeterValue);

 
 

Floats (as strings) will be rounded to integers in this approach. A good way to check if you may cleanly round what might be a float to an integer without dropping anything but zeros is:

var testing = parseFloat(ahNewMeterValue);
if (testing % 1 == 0) {
   alert('precision means nothing');
} else {
   alert('precision matters');
}

No comments:

Post a Comment