2

I have followed single object that represents location from csv and json:

 "value_location1": {
  "json_lat": "-118.3844000",
  "csv_lat": "-118.3844"
   },
  "value_location2": {
  "json_lat": "-117.933122",
  "csv_lat": "-117.933122"
},

Actually i have array of 145k (importent) of these objects and I need find good way to make value_location.csv_lat and value_location.json_lat to be equal.

As you see json_lat provides -118.3844000 when csv_lat - -118.3844.

Both have the same value as double but not as String,

What should be, high performance, way to do that?

Thanks,

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • 1
    `parseFloat(string).toFixed(4)` – adeneo Jun 16 '13 at 12:35
  • 1
    `Both have the same value as double but not as String` -> according to your javascript object that doesn't seem to be the case at all. The 2 properties are defined as strings. So you're gonna have to parse those strings to actual floating point numbers (`parseFloat`) and then round the results (`.toFixed`) – Darin Dimitrov Jun 16 '13 at 12:35
  • 1
    Note that with `toFixed()`, you will also end up with a string, not a floating point number. – Lepidosteus Jun 16 '13 at 12:38
  • @adeneo, i added other object, I know its good way but i dont want to cut off to 4 digits after dot. sometimes I can get lat or long 5-8 digits after dot and i run in loop on all 145k objects. Thanks – Maxim Shoustin Jun 16 '13 at 12:39
  • @Lepidosteus - there's always `(string*1000) / 1000` to solve that. And parseFloat would normally remove the zeros anyway. – adeneo Jun 16 '13 at 12:41
  • @adeneo: I didn't point it as an issue, but as a note to be aware of. Given the OP comments, he doesn't fully realize the difference between a number, and a string representation of a number. – Lepidosteus Jun 16 '13 at 12:42

4 Answers4

3

Your question is unclear, please learn how to ask a good question (you say you "want to make them equals" but what you want is to check if the number contained in the strings are equals, which is not the same thing at all).

Are you trying to turn these strings into floating point numbers ? Then use parseFloat(), and you can then compare them as floats (be aware of the rules)

value_location.csv_lat = parseFloat(value_location.csv_lat, 10);
// value_location.json_lat is the number -118.3844
value_location.json_lat = parseFloat(value_location.json_lat, 10);
// value_location.csv_lat is the number -118.3844

Are you trying to remove the extra 0 at the end of the strings, no matter how many digits there is after the colon ? If so, just convert them back to string after parseFloat()

value_location.csv_lat = parseFloat(value_location.csv_lat, 10) + '';
// value_location.json_lat is the string "-118.3844"
value_location.json_lat = parseFloat(value_location.json_lat, 10) + '';
// value_location.csv_lat is the string "-118.3844"
Community
  • 1
  • 1
Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
1

Just pasre values to floats and compare float values
Or you might get into this issue Javascript: Comparing two float values

Community
  • 1
  • 1
VitaliyG
  • 1,837
  • 1
  • 11
  • 11
1

Not entirely sure what you mean here. If you want this:

I need find good way to make value_location.csv_lat and value_location.json_lat to be equal.

Then value_location.csv_lat = value_location.json_lat will work a treat.

nickf
  • 537,072
  • 198
  • 649
  • 721
0

Well for starters JavaScript only has the number data type. But if you want these two numbers to equal the same string use the toFixed() method to get the amount of decimal points you want. Like this:

var example_number = 5.333000;
var rounded_number = example_number.toFixed(2);
alert(rounded_number);
Dusty
  • 1,053
  • 2
  • 11
  • 24