2

I have this code in Javascript:

var distance = turf.lineDistance(route.features[0]).toLocaleString()

In Chrome I get this result: 35.177

And in Firefox I get this result: 35,177

Why?

I need to execute this code:

document.getElementById("txtGPSDistance").value = Math.round(distance);

In Chrome it works, but in Firefox I get a NaN.

gregoryp
  • 920
  • 4
  • 15
  • 35
  • this sounds like purely a client-side thing; surely the server-side tags are just distractions? – Marc Gravell Apr 17 '18 at 21:33
  • 2
    have you tried `lineDistance(...).toFixed(3)`? – Get Off My Lawn Apr 17 '18 at 21:34
  • 1
    Related: [Number().toLocaleString() has different format in different browsers](https://stackoverflow.com/questions/29942231/number-tolocalestring-has-different-format-in-different-browsers), and [Inconsistent behavior of toLocaleString() in different browser](https://stackoverflow.com/questions/13428678/inconsistent-behavior-of-tolocalestring-in-different-browser). – showdev Apr 17 '18 at 21:36
  • @GetOffMyLawn it worked! But what exactly toFixed(3) does? And it will work in every browser? – gregoryp Apr 17 '18 at 21:40
  • The reason "why" is going to be because of [turf line distance](https://github.com/turf-junkyard/turf-line-distance) is implemented. – levininja Apr 17 '18 at 21:41
  • @mjwills yes sir – gregoryp Apr 17 '18 at 21:42
  • 1
    toLocaleString will convert to the language settings of the client browser and/or OS and you will have erratic behavior. You either want to specify a locale like 'en-US' or use toFixed() for a locale-agnostic formatting. – FBergo Apr 17 '18 at 21:42
  • @MarcGravell thank you for your comment! By other answers, just found the problem in the toLocaleString – gregoryp Apr 17 '18 at 21:43
  • this may seem totally OT (in which case, I apologize), but the OP question does have some historical relevance)....The Mars Climate Orbiter (per popular knowledge) crashed because metric was not converted to US/Imperial. In fact it was, but.... in USA and UK, we use 0,000.00 and in continental Europe we mostly use 0.000,00. Which meant that the conversions ran into 'Type Error' failures. Since that concept was difficult to explain to the public, NASA just said metric to US/Imperial. Which in turn lead to alien conspiracy theories, etc. love how simple coding errors create an industry. :-) – rockmo Apr 18 '18 at 01:57

0 Answers0