Last active
December 16, 2015 01:39
-
-
Save airspeed/5356723 to your computer and use it in GitHub Desktop.
Prüft ob die angegebene Zeichenkette eine Nummer darstellt, die ein Komma als Trennzeichen für Nachkommastellen aufweist ( Rückgabewert 0 ), ein Punkt ( Rückgabewert 1 ) oder etwas anderes ( Rückgabewert -1 ).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trennzeichen(formatted_number) | |
raise ArgumentError unless formatted_number.is_a?(String) | |
return -1 if formatted_number.split(",").count == 1 && formatted_number.split(".").count == 1 # nix | |
return 0 if formatted_number.split(",").count > 1 && formatted_number.split(".").count == 1 # bloß Komma | |
return 1 if formatted_number.split(".").count > 1 && formatted_number.split(",").count == 1 # bloß Punkt | |
# Gibt's beides, ein Komma UND einen Punkt? | |
teile = formatted_number.split(",").map { |teil| | |
next if teil.nil? | |
teil.split(".").count | |
} | |
return 0 if teile[0] > teile[1] | |
return 1 if teile[1] > teile[0] | |
return -1 | |
end | |
def parse_with_komma(formatted_number) | |
raise ArgumentError unless formatted_number.is_a?(String) | |
to_parse = trennzeichen(formatted_number) | |
case to_parse | |
when 0 | |
return formatted_number.gsub(',','.').to_d | |
when 1 | |
return formatted_number.to_d | |
when -1 | |
return formatted_number.to_d | |
else | |
raise ArgumentError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment