Longitude and latitude format conversion

official:

    public float DuFenMiaoToDu(float d, float f, float m)
    {
    
    
    	//度分秒 -> 度
        f = f + m / 60;
        d = d + f / 60;
        return d;
    }
    
    public float DuToDuFen_fen(float d)
    {
    
    
    	//度 -> 度分
        float f = (d - Mathf.Floor(d)) * 60;
        return f;
    }
    
    public float FenToFenMiao_miao(float f)
    {
    
    
    	//分 -> 分秒
        float m = (f - Mathf.Floor(f)) * 60;
        return m;
    }

Actual usage

float The number of significant digits is 6
double The number of significant digits is 16 The digits of
latitude and longitude are 8~9

Therefore, you need to use Double for data entry, but it is more convenient to use float (Mathf).
Therefore, it is decided to generally use the degree, minute and second to calculate, as follows:

	public float DuToFen(double d, double line)	//line指经纬线
    {
    
    
        float f = (float)(60 * (d - line));
        return f;
    }

eg1. d = 116.307629, line = 116
f = 60 * 0.307629 = 18.4577

eg2. d = 113.947906, line = 114
f = 60 * -0.052094 = -3.1256

Update:

public string GetStringLongitude(double value)
{
    
    
    float du = Mathf.Floor((float)value);
    double f = 60 * (value - du);
    float fen = Mathf.Floor((float)f);
    double m = 60 * (f - fen);
    float miao = Mathf.Floor((float)m);

    string str = du + "°" + fen + "' " + miao + "\" E";
    return str;
}
public string GetStringLatitude(double value)
{
    
    
    float du = Mathf.Floor((float)value);
    double f = 60 * (value - du);
    float fen = Mathf.Floor((float)f);
    double m = 60 * (f - fen);
    float miao = Mathf.Floor((float)m);

    string str = du + "°" + fen + "' " + miao + "\" N";
    return str;
}

Reference

Longitude and Latitude Format Conversion Tool

Guess you like

Origin blog.csdn.net/MikeW138/article/details/100297035