SHARE
TWEET

Untitled

a guest Nov 15th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QString>
  2. #include <QStringList>
  3. #include <cmath>
  4. #include <cassert>
  5.  
  6. QString toString(double angleInRad, int precision)
  7. {
  8.     const auto angleInDeg = std::abs(angleInRad*180/M_PI);
  9.     auto degrees = static_cast<int>(angleInDeg);
  10.     auto minutes = static_cast<int>((angleInDeg-degrees)*60);
  11.     const auto seconds = ((angleInDeg-degrees)*60-minutes)*60;
  12.  
  13.     const auto secStr = QString::number(seconds, 'f', precision);
  14.     auto secStrIntAndFrac = secStr.split('.');
  15.     if(precision==0)
  16.         assert(secStrIntAndFrac.size()==1);
  17.     else
  18.         assert(secStrIntAndFrac.size()==2);
  19.  
  20.     // Rounding on converison to string may have resulted in 60 seconds, which should be carried to minutes
  21.     if(secStrIntAndFrac[0]=="60")
  22.     {
  23.         secStrIntAndFrac[0]="0";
  24.         ++minutes;
  25.     }
  26.     // Carry to minutes may have resulted in 60 minutes, which should be carried to degrees
  27.     if(minutes==60)
  28.     {
  29.         minutes=0;
  30.         ++degrees;
  31.     }
  32.  
  33.     const auto intSecStr  = secStrIntAndFrac[0].rightJustified(2, '0');
  34.     const auto fracSecStr = precision==0 ? "" : "."+secStrIntAndFrac[1];
  35.  
  36.     return QString::fromUtf8(u8"%1°%2′%3″")
  37.                   .arg(degrees, 3, 10, QChar('0'))
  38.                   .arg(minutes, 2, 10, QChar('0'))
  39.                   .arg(intSecStr+fracSecStr);
  40. }
RAW Paste Data
Top