Jump to content

Formatting minutes part of waypoint


Geofellas

Recommended Posts

I am not that familiar with the format strings used by the string.format function - I have currently created a rather unsatisfactory (yet working) bit of code to format the minutes part of a coordinate in the way we are used to - i.e. 00.000 (rounded to 3 decimal places with leading and trailing zeroes included)

 

Here is the code that formats the entire latitude portion as currently implemented:

 

local l = math.abs(lat)
local deg = math.floor(l)
local min = 60*(l - deg) 
local minint = math.floor(min)
local mindec = min-minint+ 0.0005
if lat < 0 then
cout = "S "
else
cout = "N "
end
cout = cout .. string.format("%2i",deg) .. [[ ]] .. string.format("%.2d",minint) .. [[.]] .. string.format("%.3i", mindec*1000)

 

I would be grateful for any suggestions for improvement as this seems rather inelegant

 

Thanks

Link to comment

You can make the code slightly shorter using the function "math.modf" to split off the integer and decimal components of the degrees.

 

%2.3f should give you the right formatting for the minutes (integer and decimal together).

 

Also, you can run all of the formatting together in a single string.format call:

string.format("%2i %3.2f",deg,min)

 

And if you want to save lines, the hemisphere letter can be assigned like this:

cout = (lat < 0) and "S" or "N"

 

(I'm writing this without Lua in front of me, so I don't guarantee that the syntax is perfect.)

Edited by sTeamTraen
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...