0

How to write the equivalent PL/SQL function from this Delphi function:

Function TConvert.MGRS(lat : Double; Lon : Double; a : Double; 
         InverseFlattening : Double; Coding:Integer; Digits : Integer) : String ;
var
 UTMs1 : String;
 E1 : Double;
 N1 : Double;
 Zn : Integer;
 Lzn : String;
 Sq : String;
begin
 UTMs1 := UTM(lat, Lon, a, InverseFlattening) ;
 E1 := UTMX(UTMs1);
 N1 := UTMY(UTMs1);
 Zn := UTMZone(lat, Lon);
 Lzn := MGRSLatZone(lat);
 Sq := SquareID(Zn, N1, E1, Coding);
 Result := Format('%.2d', [Zn] )  + Lzn + Sq +
     Copy(Format('%.5d',[Round(E1 - 100000 * Trunc(E1 / 100000))]), 1, Digits) +
     Copy(Format('%.5d',[Round(N1 - 100000 * Trunc(N1 / 100000))]), 1, Digits);
end;

MGRS(0, 0, 6378.137, 298.2572236, 1, 5) = 31NAA6602100000

4
  • 1
    Please supply full function definition with parameters. Commented Sep 12, 2013 at 11:19
  • Some samples of expected inputs and output would also be useful. Commented Sep 12, 2013 at 11:38
  • Do you know any PL/SQL? Have you tried to write any code? Do you understand what this code does? Commented Sep 12, 2013 at 11:45
  • @David Heffernan Yes I know some PL/SQL. Yes, almost done writing. Yes, but I am not able to understand how to format it in PL/SQL. Commented Sep 12, 2013 at 12:00

1 Answer 1

4

Not sure if entire code may be translated to native Oracle calls, but regarding initial question about string formatting resulting code may look like this:

create or replace function MyFunction(
  Zn     in number,
  Lzn    in varchar2,
  Sq     in varchar2,
  E1     in number,
  N1     in number,
  Digits in number
) 
return varchar2
as
  vRes varchar2(4000);
begin

  vRes := trim(to_char(Zn,'999999999999999999.00')) || LZn || Sq || 
          substr(trim(to_char(round(E1 - 100000 * trunc(E1/100000)),'999999999999999999.00000')), 1, Digits) || 
          substr(trim(to_char(round(N1 - 100000 * trunc(N1/100000)),'999999999999999999.00000')), 1, Digits) 
  ;

  return vRes;

end;

SQLFiddle test

Oracle format models

TO_CHAR()

TRUNC()

TRIM()

Google ;)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. You got a few seconds ahead of me, I was about to answer my own question with something like: RT*M letmegooglethatforyou.com/?q=pl/sql+format+mask
@writtrup Ok :) Note that number formats always return leading blank, so you need trim output.
Complete code can be reviewed here: gis.stackexchange.com/questions/71387/… Thank you @ThinkJet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.