View Single Post
  #2 (permalink)  
Old February 21st, 2017, 08:02 PM
canolyb1 canolyb1 is offline
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Feb 2017
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

parseFloat is working fine however if the value is after the decimal is 0 the it will not be converted. If it was any number more than 0 it would be converted to a decimal.

You can test this by changing the verion to .1 o .2 and you'll see the scring is converting to a number with a floating point fine.

If you absolutely need to see the 0 then you will have to add a toFixed() method.

Also it seems to me your substring is cont capturing the whole version. Change the sunbstring to substring(findingIndex, findingIndex+4).

So the total code will look like this:

HTML Code:
var browserString = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)";
var browserName = "MSIE";
var findingIndex = browserString.indexOf(browserName) + browserName.length + 1;
var browserVersion = parseFloat(browserString.substring(findingIndex, findingIndex+4)).toFixed(1);
Reply With Quote