File tree Expand file tree Collapse file tree 2 files changed +10
-0
lines changed
Expand file tree Collapse file tree 2 files changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,15 @@ defmodule Float do
2222
2323 """
2424 @ spec parse ( binary ) :: { float , binary } | :error
25+
26+ # Integer.parse will parse "-0" as 0, so it needs to be handled separately
27+ def parse ( << "-0." , rest :: binary >> ) do
28+ case parse ( << ?. , rest :: binary >> , 0 ) do
29+ :error -> :error
30+ { parsed , remainder } -> { - parsed , remainder }
31+ end
32+ end
33+
2534 def parse ( binary ) when is_binary ( binary ) do
2635 case Integer . parse binary do
2736 :error -> :error
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ defmodule FloatTest do
66 test :parse do
77 assert Float . parse ( "12" ) === { 12.0 , "" }
88 assert Float . parse ( "-12" ) === { - 12.0 , "" }
9+ assert Float . parse ( "-0.1" ) === { - 0.1 , "" }
910 assert Float . parse ( "123456789" ) === { 123456789.0 , "" }
1011 assert Float . parse ( "12.5" ) === { 12.5 , "" }
1112 assert Float . parse ( "12.524235" ) === { 12.524235 , "" }
You can’t perform that action at this time.
0 commit comments