Code Library

M · UDF

fnGrausParaDecimais

Converte coordenada em graus, minutos e segundos para grau decimal.

Categoria: GeografiaTipo: UDFVersão: v2.0Fonte: Paulo André Jesus — OfficeTuningAtualizado em: 25/09/2026
Documentação completa na Function Library →

Sintaxe

fnGrausParaDecimais(graus as nullable text) as nullable number

Argumentos

NomeTipoObrigatórioDescrição
grausnullable textSimCoordenada no formato G°M'S"D, com D = N, S, E, L, W ou O.

Retorno

nullable number

Código completo

let
    fnGrausParaDecimais = (graus as nullable text) as nullable number =>
        let
            // Padroniza símbolos: ′ ’ → '   ″ ” '' → "   vírgula → ponto
            vTexto = Text.Upper(Text.Trim(List.Accumulate(
                {{"′", "'"}, {"’", "'"}, {"″", """"}, {"”", """"}, {"''", """"}, {",", "."}},
                graus ?? "",
                (t, par) => Text.Replace(t, par{0}, par{1})
            ))),
            fnNumero = (t as text) as number => Number.FromText(Text.Trim(t), "en-US"),
            vGraus = Number.Abs(fnNumero(Text.BeforeDelimiter(vTexto, "°"))),
            vMinutos = fnNumero(Text.BetweenDelimiters(vTexto, "°", "'")),
            vSegundos = fnNumero(Text.BetweenDelimiters(vTexto, "'", """")),
            vNegativo = List.Contains({"S", "W", "O"}, Text.End(vTexto, 1)) or Text.StartsWith(vTexto, "-")
        in
            if graus = null then null
            else (vGraus + vMinutos / 60 + vSegundos / 3600) * (if vNegativo then -1 else 1),

    Documentacao = [
        Documentation.Name = "fnGrausParaDecimais",
        Documentation.Description = "Converte coordenada em graus, minutos e segundos para grau decimal.",
        Documentation.LongDescription = "Formato esperado: G°M'S""D, com D = N, S, E, L, W ou O. S, W, O ou sinal negativo geram valor negativo. Lê números com ponto ou vírgula decimal, independente da cultura do arquivo.",
        Documentation.Category = "Geografia",
        Documentation.Author = "Paulo André Jesus",
        Documentation.Version = "2.0",
        Documentation.Examples = {
            [Description = "Latitude sul", Code = "fnGrausParaDecimais(""23°34'45""""S"")", Result = "-23.5791666666667"],
            [Description = "Longitude oeste", Code = "fnGrausParaDecimais(""46°37'14""""W"")", Result = "-46.6205555555556"],
            [Description = "Latitude norte", Code = "fnGrausParaDecimais(""12°15'30""""N"")", Result = "12.2583333333333"]
        }
    ]
in
    Value.ReplaceType(fnGrausParaDecimais, Value.ReplaceMetadata(Value.Type(fnGrausParaDecimais), Documentacao))

Exemplos

fnGrausParaDecimais("23°34'45""S")
// -23.5791666666667

fnGrausParaDecimais("46°37'14""W")
// -46.6205555555556

fnGrausParaDecimais("12°15'30""N")
// 12.2583333333333

Dicas e observações

💡 S, W, O (oeste) ou um sinal negativo no início geram valor negativo. 💡 Aceita ′ ″ tipográficos e vírgula decimal nos segundos; a leitura não depende da cultura do arquivo.