decimal2sexagesimal function

String decimal2sexagesimal(
  1. double dec
)

Converts a decimal coordinate value to sexagesimal format

final String sexa1 = decimal2sexagesimal(51.519475);
expect(sexa1, '51° 31\' 10.11"');

final String sexa2 = decimal2sexagesimal(-42.883891);
expect(sexa2, '42° 53\' 02.01"');

Implementation

String decimal2sexagesimal(final double dec) {
  final buf = StringBuffer();

  final absDec = dec.abs();
  final deg = absDec.floor();
  buf.write(deg.toString() + '°');

  final mins = (absDec - deg) * 60.0;
  final min = mins.floor();
  buf.write(' ' + zeroPad(min) + "'");

  final secs = (mins - mins.floorToDouble()) * 60.0;
  final sec = secs.floor();
  final frac = ((secs - secs.floorToDouble()) * 100.0).round();
  buf.write(' ' + zeroPad(sec) + '.' + zeroPad(frac) + '"');

  return buf.toString();
}