diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso index dfc0f655e539..91a40f88d28e 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso @@ -794,13 +794,20 @@ type Float Float.parse "(123,456,789.654)" format="###,###.##;(###,###.##)" # => -123456789.654 parse : Text -> Locale | Nothing -> Float ! Number_Parse_Error ! Illegal_Argument - parse text locale:Locale=Locale.default format:Text="" -> Float ! Number_Parse_Error ! Illegal_Argument = - Illegal_Argument.handle_java_exception <| - # `getInstance` returns `DecimalFormat` or a subclass of `DecimalFormat`. - decimal_format = NumberFormat.getInstance locale.java_locale - decimal_format.applyLocalizedPattern format - Panic.catch ParseException (decimal_format.parse text) _-> - Error.throw (Number_Parse_Error.Error text) + parse text (locale : Locale | Nothing = Nothing) (format : Text | Nothing = Nothing) -> Float ! Number_Parse_Error ! Illegal_Argument = + case locale.is_nothing && format.is_nothing of + True -> + Panic.catch NumberFormatException (Double.parseDouble text) _-> + Error.throw (Number_Parse_Error.Error text) + False -> + Illegal_Argument.handle_java_exception <| + defaulted_locale = locale.if_nothing Locale.default + defaulted_format = format.if_nothing "" + # `getInstance` returns `DecimalFormat` or a subclass of `DecimalFormat`. + decimal_format = NumberFormat.getInstance defaulted_locale.java_locale + decimal_format.applyLocalizedPattern defaulted_format + Panic.catch ParseException (decimal_format.parse text) _-> + Error.throw (Number_Parse_Error.Error text) ## ICON input_number diff --git a/test/Base_Tests/src/Data/Numbers_Spec.enso b/test/Base_Tests/src/Data/Numbers_Spec.enso index e86ed651ac97..4a66dc988ae6 100644 --- a/test/Base_Tests/src/Data/Numbers_Spec.enso +++ b/test/Base_Tests/src/Data/Numbers_Spec.enso @@ -303,7 +303,7 @@ add_specs suite_builder = Float.parse "aaaa" l . should_fail_with Number_Parse_Error group_builder.specify "should parse correctly with format and/or locale" <| - Float.parse "123,456,789.87654" . should_equal 123456789.87654 + Float.parse "123,456,789.87654" locale=Locale.default . should_equal 123456789.87654 Float.parse "123.456.789,87654" locale=Locale.italy . should_equal 123456789.87654 Float.parse "123,456,789.88" format="#,###.##" . should_equal 123456789.88