Skip to content

Instantly share code, notes, and snippets.

@tatey
Created October 9, 2010 10:16
Show Gist options
  • Save tatey/618087 to your computer and use it in GitHub Desktop.
Save tatey/618087 to your computer and use it in GitHub Desktop.
class String
# Converts degrees, minutes and seconds to decimal degrees
#
# "52°12′17.0″N".to_degrees # => 52.2047222222222
# "27 28 05S".to_degrees # => -27.4680555555556
# "Invalid".to_degrees # => 0.0
#
# Returns Float
def to_degrees
self =~ /^(\d+)[^\d]+(\d+)[^\d]+([\d\.]+)[^NESW]*([NESW])/i
deg = $1.to_f + ($2.to_f / 60) + ($3.to_f / 3600)
$4 =~ /[SW]/i ? -deg : +deg
end
end
puts "52°12′17.0″N".to_degrees
puts "27 28 05S".to_degrees
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment