How to calculate distance in (km) is lower then given distance in string (km)

HI

I want to calculate distance is within the range of 1.0 km.
Iam using google distanematrix to get the distance in km to the place, and its working fine.

function returns a string, witch looks like f.eks:
- "0.9 km"
- "6.9 km"

I want to calculate so that distance is within the range of 1.0 km and not higher, but lower is ok...
But some times i also get distance in meter when iam close to the place, f.eks:
"14 m"
"20 m"

So how can i calculate correct distance if i get distance in double string value or as an int value?

Here is my if statement where iam checking the returned distance in km is lower then "1.0 km"
var dist = str.split(" "); //Split so i can only get the value and not the string "km"
if (Math.round(dist[0] * 100) <= Math.round('1.0' * 100)) {
                         alert('you are in the range')
                        }
else{
alert('you are to far away')
}

Open in new window





iam not getting the correct result with the code above, any suggestion for how i can calculate the correct way will be really appriciated...

Thanks in advance
AdnanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
Comments inside the js code:
//I use the parseFloat function to get a float number
var dist = parseFloat(str.split(" ")); //Split so i can only get the value and not the string "km"
if(dist<=1.0){
  alert('you are in the range');
}else{
  alert('you are to far away');
}

Open in new window

0
AdnanAuthor Commented:
Thanks for the response...

but what about if i get distance in meters f.eks ("20 m") and not km (4.4) ?
0
Leonidas DosasCommented:
var index=str.substr(-2);

switch (index){
  case " m":
   var dist = parseInt(str.split(" "));    
   dist=parseFloat(dist/1000);
   if(dist<=1.0){
   alert('you are in the range');
     break;
   }else{
   alert('you are to far away');
     break;
   }
  case "km":
  //I use the parseFloat function to get a float number
  var dist = parseFloat(str.split(" ")); //Split so i can only get the value and not the string "km"
  if(dist<=1.0){
  alert('you are in the range');
    break;
  }else{
  alert('you are to far away');
    break;
}
}
  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
<script>
var dist = "43 m";

var result = dist.match(/^(\d+[\.]?[\d+]?)\s+(.*)$/);
if (result) {
  // convert to meters
  var newdist = result[1] * (result[2] == 'km' ? 1000 : 1);
  console.log(newdist);
}
</script>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Angular

From novice to tech pro — start learning today.