+Geofellas Posted September 8, 2008 Share Posted September 8, 2008 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 Quote Link to comment
+sTeamTraen Posted September 9, 2008 Share Posted September 9, 2008 (edited) 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 September 9, 2008 by sTeamTraen Quote Link to comment
+Geofellas Posted September 10, 2008 Author Share Posted September 10, 2008 You can make the code slightly shorter... Thanks so much - this will be much nicer Quote Link to comment
Recommended Posts
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.