Help: Pascal to Lua translation please..

Can somebody please give me a hand with this:

I am new to Lua and need to convert a Longitude (GPS) Double to a specific string representation.

I am trying to convert my Pascal routines to lua.

I got this far:

function DegreesToLongStr(LongD)  – Where LongD is a double and result is a string
local AChar = “”
local D = 0
local M = 0
local Dstr = “”
local MStr = “”

  if LongD < 0 – Its negative
  then
      AChar = “W”
      LongD = -LongD – make it positive
  else
      AChar = “E”
  end    
    
  d = trunc(LongD) – I cannot find this in Lua. if LongD = 123.456789 then d = 123 and m = .456789
  m = frac(LongD)*60

  Dstr = IntToStr(d)  – convert integer d to string Dstr
  while string.len(Dstr) < 3 do DStr = “0”…DStr end – hope this works. Needs leading zeros.
  Mstr = FloatToStrF(m,ffFixed,4,2) – convert double m to formatted string
  while string.len(MStr) < 5 do MStr = “0”…Mstr end
  return = Dstr…Mstr…Achar
end
 

result should be: “12327.40W” …I think.

Any help appreciated…

I probably would try converting LongD to string using tostring() . You can always get it back with tonumber()

You can the use string.match() to look for - at position 1

You can also use string.sub() to deal with trunc like processing.

http://docs.coronalabs.com/api/library/string/match.html

http://docs.coronalabs.com/api/library/string/sub.html

Hope this helps. Best of luck.

No, I first need to calculate the Fractional of the degrees (*60) because it must be in Minutes and it is in tenths of a Degree.

I really need to to the calculations first before I change it to strings.

No problems. Turn to string, use string.match and string.sub to break your long into the required parts, then turn them back to number individually to do your math work. Would that not work?

Lua will freely convert strings to numbers and numbers to strings so you don’t explicitly have to do that though there are tonumber() and tostring() functions.

function DegreesToLongStr(LongD)&nbsp; -- Where LongD is a double and result is a string &nbsp;&nbsp;&nbsp; local AChar = "E" &nbsp;&nbsp;&nbsp; local D = 0 &nbsp;&nbsp;&nbsp; local M = 0 &nbsp;&nbsp;&nbsp; local Dstr = "" &nbsp;&nbsp;&nbsp; local MStr = "" &nbsp;&nbsp;&nbsp; if LongD \< 0 then -- negative &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; AChar = "W" &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; LongD = LongD&nbsp; \* -1 -- make it positive, Lua doesn't support urinary operators. &nbsp;&nbsp;&nbsp; end&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --d = trunc(LongD) -- I cannot find this in Lua. if LongD = 123.456789 then d = 123 and m = .456789 &nbsp;&nbsp;&nbsp; d = math.floor(LongD) -- use math.\* API's for this. &nbsp;&nbsp;&nbsp; --m = frac(LongD)\*60 &nbsp;&nbsp;&nbsp; m = (LongD - d) \* 60 &nbsp;&nbsp;&nbsp; Dstr = tostring(d)&nbsp; -- convert integer d to string Dstr &nbsp;&nbsp;&nbsp; -- while string.len(Dstr) \< 3 do DStr = "0"..DStr end -- hope this works. Needs leading zeros -- should work but do this instead. &nbsp;&nbsp;&nbsp; Dstr = string.format("%02d", d) &nbsp;&nbsp;&nbsp; --Mstr = FloatToStrF(m,ffFixed,4,2) -- convert double m to formatted string &nbsp;&nbsp;&nbsp; --while string.len(MStr) \< 5 do MStr = "0"..Mstr end &nbsp;&nbsp;&nbsp; Mstr = string.format("%04.2f", m) &nbsp;&nbsp;&nbsp; return = Dstr..Mstr..Achar&nbsp; -- don't you need something between the degrees and minutes string? end

Thats great, thanks!

I probably would try converting LongD to string using tostring() . You can always get it back with tonumber()

You can the use string.match() to look for - at position 1

You can also use string.sub() to deal with trunc like processing.

http://docs.coronalabs.com/api/library/string/match.html

http://docs.coronalabs.com/api/library/string/sub.html

Hope this helps. Best of luck.

No, I first need to calculate the Fractional of the degrees (*60) because it must be in Minutes and it is in tenths of a Degree.

I really need to to the calculations first before I change it to strings.

No problems. Turn to string, use string.match and string.sub to break your long into the required parts, then turn them back to number individually to do your math work. Would that not work?

Lua will freely convert strings to numbers and numbers to strings so you don’t explicitly have to do that though there are tonumber() and tostring() functions.

function DegreesToLongStr(LongD)&nbsp; -- Where LongD is a double and result is a string &nbsp;&nbsp;&nbsp; local AChar = "E" &nbsp;&nbsp;&nbsp; local D = 0 &nbsp;&nbsp;&nbsp; local M = 0 &nbsp;&nbsp;&nbsp; local Dstr = "" &nbsp;&nbsp;&nbsp; local MStr = "" &nbsp;&nbsp;&nbsp; if LongD \< 0 then -- negative &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; AChar = "W" &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; LongD = LongD&nbsp; \* -1 -- make it positive, Lua doesn't support urinary operators. &nbsp;&nbsp;&nbsp; end&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --d = trunc(LongD) -- I cannot find this in Lua. if LongD = 123.456789 then d = 123 and m = .456789 &nbsp;&nbsp;&nbsp; d = math.floor(LongD) -- use math.\* API's for this. &nbsp;&nbsp;&nbsp; --m = frac(LongD)\*60 &nbsp;&nbsp;&nbsp; m = (LongD - d) \* 60 &nbsp;&nbsp;&nbsp; Dstr = tostring(d)&nbsp; -- convert integer d to string Dstr &nbsp;&nbsp;&nbsp; -- while string.len(Dstr) \< 3 do DStr = "0"..DStr end -- hope this works. Needs leading zeros -- should work but do this instead. &nbsp;&nbsp;&nbsp; Dstr = string.format("%02d", d) &nbsp;&nbsp;&nbsp; --Mstr = FloatToStrF(m,ffFixed,4,2) -- convert double m to formatted string &nbsp;&nbsp;&nbsp; --while string.len(MStr) \< 5 do MStr = "0"..Mstr end &nbsp;&nbsp;&nbsp; Mstr = string.format("%04.2f", m) &nbsp;&nbsp;&nbsp; return = Dstr..Mstr..Achar&nbsp; -- don't you need something between the degrees and minutes string? end

Thats great, thanks!