Skip to content

Instantly share code, notes, and snippets.

@mando
Last active January 21, 2016 00:51
Show Gist options
  • Save mando/c33cb129af8f2f57cf25 to your computer and use it in GitHub Desktop.
Save mando/c33cb129af8f2f57cf25 to your computer and use it in GitHub Desktop.
Decimal degrees to minute/seconds degrees
class DecimalDegree
# logic from http://mathforum.org/sarah/hamilton/ham.degrees.html
attr_accessor :decimal
def initialize(decimal)
@decimal = decimal.to_f
end
def convert_to_min_sec
degrees = decimal.to_int
minutes = ((decimal - degrees) * 60).abs
seconds = ((minutes - minutes.to_int) * 60).abs.round(2)
puts "#{degrees} #{minutes.to_int}\' #{seconds}\""
end
end
if __FILE__ == $0
DecimalDegree.new(ARGV.first).convert_to_min_sec
end
$ ruby decimal_degree.rb -27.116667
-27 7' 0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment