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"');

Implementation

String decimal2sexagesimal(final double dec) {
  List<int> _split(final double value) {
    // NumberFormat is necessary to create digit after comma if the value
    // has no decimal point (only necessary for browser)
    final List<String> tmp = new NumberFormat("0.0#####")
        .format(round(value, decimals: 10))
        .split('.');
    return <int>[int.parse(tmp[0]).abs(), int.parse(tmp[1])];
  }

  final List<int> parts = _split(dec);
  final int integerPart = parts[0];
  final int fractionalPart = parts[1];

  final int deg = integerPart;
  final double min = double.parse("0.${fractionalPart}") * 60;

  final List<int> minParts = _split(min);
  final int minFractionalPart = minParts[1];

  final double sec = (double.parse("0.${minFractionalPart}") * 60);

  return "${deg}° ${min.floor()}' ${round(sec, decimals: 2).toStringAsFixed(2)}\"";
}