输入的经纬度是 118.8000745,转换为度分秒的格式
计算:度 就是 118度;
分 用小数部分0.8000745 * 60 = 48.00447,分就是48分;
秒 用分的小数部分 0.00447 * 60 = 0.2682 秒
写了个QAngle类,头文件qangle.h
1 #ifndef QANGLE_H 2 #define QANGLE_H 3 4 #include <QObject> 5 6 class QAngle : public QObject 7 { 8 Q_OBJECT 9 public: 10 QAngle(QObject *parent); 11 QAngle(double angle); 12 QAngle(int nDegree, int nMinute,int nSecond); 13 QString getDDMMSS(); 14 ~QAngle(); 15 private: 16 double dAngle;//dAngle——角度, 单位度(°) 17 int nDegree; 18 int nMinute; 19 int nSecond; 20 void ConvertToAngle(); 21 void ConvertToddmmss( int nPrecision); 22 }; 23 24 #endif // QANGLE_H
实现文件qangle.cpp
1 #include "qangle.h" 2 3 QAngle::QAngle(QObject *parent) 4 : QObject(parent) 5 { 6 7 } 8 QAngle::QAngle(double angle) 9 { 10 dAngle=angle; 11 ConvertToddmmss(3); 12 } 13 QAngle::QAngle(int _Degree, int _Minute,int _Second) 14 { 15 nDegree=_Degree; 16 nMinute=_Minute; 17 nSecond=_Second; 18 ConvertToAngle(); 19 } 20 void QAngle::ConvertToAngle() 21 { 22 dAngle=nDegree+nMinute/60+nSecond/3600; 23 } 24 QString QAngle::getDDMMSS() 25 { 26 QString strDDMMSS; 27 strDDMMSS.append(QString::number(nDegree)); 28 strDDMMSS.append(QString::fromLocal8Bit("°")); 29 strDDMMSS.append(QString::number(nMinute)); 30 strDDMMSS.append(tr("'")); 31 strDDMMSS.append(QString::number(nSecond)); 32 strDDMMSS.append(tr(""")); 33 return strDDMMSS; 34 } 35 //nPrecision——精度, 1:度、2:分、3:秒 36 void QAngle::ConvertToddmmss(int nPrecision) 37 { 38 if (1 == nPrecision) // 度 39 { 40 int nDegree = int(dAngle); 41 if (fabs(dAngle - nDegree) > 0.5) 42 { 43 // 四舍五入 44 nDegree += 1; 45 } 46 if (nDegree > 360) 47 { 48 nDegree -= 360; 49 } 50 //strDimText.Format(_T("%d°"), nDegree); 51 } 52 else if (2 == nPrecision) // 分 53 { 54 int nDegree = int(dAngle); 55 int nMinute = int((dAngle - nDegree) * 60); 56 if (fabs((dAngle - nDegree) * 60 - nMinute) > 0.5) 57 { 58 // 四舍五入 59 nMinute += 1; 60 } 61 if (nMinute >= 60) 62 { 63 nMinute = 0; 64 nDegree += 1; 65 } 66 if (nDegree > 360) 67 { 68 nDegree -= 360; 69 } 70 //strDimText.Format(_T("%d°%d'"), nDegree, nMinute); 71 } 72 else if (3 == nPrecision) // 秒 73 { 74 nDegree = int(dAngle); 75 nMinute = int((dAngle - nDegree) * 60); 76 nSecond = int(((dAngle - nDegree) * 60 - nMinute) * 60); 77 78 if (fabs(((dAngle - nDegree) * 60 - nMinute) * 60 - nSecond) > 0.5) 79 { 80 // 四舍五入 81 nSecond += 1; 82 } 83 if (nSecond >= 60) 84 { 85 nSecond = 0; 86 nMinute += 1; 87 } 88 if (nMinute >= 60) 89 { 90 nMinute = 0; 91 nDegree += 1; 92 } 93 if (nDegree > 360) 94 { 95 nDegree -= 360; 96 } 97 //strDimText.Format(_T("%d°%d'%d""), nDegree, nMinute, nSecond); 98 } 99 } 100 QAngle::~QAngle() 101 { 102 103 }
最新评论