Search code examples
sqloracle-databaserounding

Euclidean Distance


Hacker Rank - Weather Observation Station 19

Given a table STATION that holds data for five fields namely ID, CITY, STATE, NORTHERN LATITUDE and WESTERN LONGITUDE.

+-------------+------------+
| Field       |   Type     |
+-------------+------------+
| ID          | INTEGER    |
| CITY        | VARCHAR(21)|
| STATE       | VARCHAR(2) |
| LAT_N       | NUMERIC    |
| LONG_W      | NUMERIC    |
+-------------+------------+

Consider P1(a, b) and P2(c, d) be two points on 2D plane, where (a, b) be minimum and maximum values of Northern Latitude and (c, d) be minimum and maximum values of Western Longitude. Write a query to print the Euclidean Distance between points P1 and P2 up to 4 decimal digits.

my solution for oracle is :

select round((abs(max(lat_n)-max(long_w)) + abs(min(long_w)-min(lat_n))),4) from station;  

but it is giving answer to the 3 decimal points but i need 4 decimal points.


Solution

  • Try this ltrim(to_char(x, '999.9999'))

    Number Format Models