mirror of
https://github.com/enso-org/enso.git
synced 2025-01-03 16:03:19 +03:00
Add format parameter to Float.parse (#11229)
This commit is contained in:
parent
cce50fab3a
commit
47bf591b5a
@ -75,6 +75,7 @@
|
|||||||
- [Extend the range of `floor`, `ceil`, `trunc` to values outside the `Long`
|
- [Extend the range of `floor`, `ceil`, `trunc` to values outside the `Long`
|
||||||
range.][11135]
|
range.][11135]
|
||||||
- [Added `format` parameter to `Decimal.parse`.][11205]
|
- [Added `format` parameter to `Decimal.parse`.][11205]
|
||||||
|
- [Added `format` parameter to `Float.parse`.][11229]
|
||||||
|
|
||||||
[10614]: https://github.com/enso-org/enso/pull/10614
|
[10614]: https://github.com/enso-org/enso/pull/10614
|
||||||
[10660]: https://github.com/enso-org/enso/pull/10660
|
[10660]: https://github.com/enso-org/enso/pull/10660
|
||||||
@ -89,6 +90,7 @@
|
|||||||
[11120]: https://github.com/enso-org/enso/pull/11120
|
[11120]: https://github.com/enso-org/enso/pull/11120
|
||||||
[11135]: https://github.com/enso-org/enso/pull/11135
|
[11135]: https://github.com/enso-org/enso/pull/11135
|
||||||
[11205]: https://github.com/enso-org/enso/pull/11205
|
[11205]: https://github.com/enso-org/enso/pull/11205
|
||||||
|
[11229]: https://github.com/enso-org/enso/pull/11229
|
||||||
|
|
||||||
#### Enso Language & Runtime
|
#### Enso Language & Runtime
|
||||||
|
|
||||||
|
@ -750,23 +750,57 @@ type Float
|
|||||||
GROUP Conversions
|
GROUP Conversions
|
||||||
ICON convert
|
ICON convert
|
||||||
|
|
||||||
Parses a textual representation of a float into a float number, returning
|
Parses a string into a `Float`, returning a `Number_Parse_Error` if the
|
||||||
a `Number_Parse_Error` if the text does not represent a valid float.
|
text does not represent a valid `Float`.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
- text: The text to parse into a float.
|
- text: The text to parse into a `Float`.
|
||||||
- locale: The locale that specifies the format to use when parsing
|
- locale: The locale that specifies the format to use when parsing.
|
||||||
|
- format: The Java-style format to use to parse the string.
|
||||||
|
|
||||||
|
! Error Conditions
|
||||||
|
|
||||||
|
- If `text` is incorrectly formatted, a `Number_Parse_Error` is thrown.
|
||||||
|
- If `format` is incorrectly formatted (or uses digit separators that
|
||||||
|
differ from the ones specified by the locale), an `Illegal_Argument`
|
||||||
|
is thrown.
|
||||||
|
|
||||||
> Example
|
> Example
|
||||||
Parse the text "7.6" into a float number.
|
Parse a `Float` with no locale specifier.
|
||||||
|
|
||||||
Float.parse "7.6"
|
Float.parse "123456789.87654"
|
||||||
parse : Text -> Locale | Nothing -> Float ! Number_Parse_Error
|
# => 123456789.87654
|
||||||
parse text locale=Nothing = case locale of
|
|
||||||
Nothing -> Panic.catch NumberFormatException (Double.parseDouble text) _->
|
> Example
|
||||||
Error.throw (Number_Parse_Error.Error text)
|
Parse a `Float` with the default locale.
|
||||||
Locale.Value java_locale -> Panic.catch ParseException ((NumberFormat.getInstance java_locale).parse text) _->
|
|
||||||
Error.throw (Number_Parse_Error.Error text)
|
Float.parse "123,456,789.87654" locale=Locale.default
|
||||||
|
# => 123456789.87654
|
||||||
|
|
||||||
|
> Example
|
||||||
|
Parse a `Float` with the US locale.
|
||||||
|
|
||||||
|
Float.parse "123,456,789.87654" locale=Locale.us
|
||||||
|
# => 123456789.87654
|
||||||
|
|
||||||
|
> Example
|
||||||
|
Parse a `Float` with the Italy locale.
|
||||||
|
|
||||||
|
Float.parse "123.456.789,87654" locale=Locale.italy
|
||||||
|
# => 123456789.87654
|
||||||
|
> Example
|
||||||
|
Parse a `Float` with an explicit negative number format.
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
## ICON input_number
|
## ICON input_number
|
||||||
|
|
||||||
|
@ -1536,7 +1536,7 @@ Text.last_index_of self term="" start=-1 case_sensitivity=Case_Sensitivity.Sensi
|
|||||||
"7.6".parse_float
|
"7.6".parse_float
|
||||||
@locale Locale.default_widget
|
@locale Locale.default_widget
|
||||||
Text.parse_float : Locale | Nothing -> Float ! Number_Parse_Error
|
Text.parse_float : Locale | Nothing -> Float ! Number_Parse_Error
|
||||||
Text.parse_float self locale=Nothing = Float.parse self locale
|
Text.parse_float self locale=Locale.default = Float.parse self locale
|
||||||
|
|
||||||
## ALIAS integer from text, to_integer
|
## ALIAS integer from text, to_integer
|
||||||
GROUP Conversions
|
GROUP Conversions
|
||||||
|
@ -302,6 +302,28 @@ add_specs suite_builder =
|
|||||||
Float.parse "000000,0001" l . should_equal 0.0001
|
Float.parse "000000,0001" l . should_equal 0.0001
|
||||||
Float.parse "aaaa" l . should_fail_with Number_Parse_Error
|
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.italy . should_equal 123456789.87654
|
||||||
|
|
||||||
|
Float.parse "123,456,789.88" format="#,###.##" . should_equal 123456789.88
|
||||||
|
Float.parse "123.456.789,88" format="#.###,##" locale=Locale.italy . should_equal 123456789.88
|
||||||
|
Float.parse "1,23,45,67,89.88" format="#,##.##" . should_equal 123456789.88
|
||||||
|
Float.parse "1.23.45.67.89,88" format="#.##,##" locale=Locale.italy . should_equal 123456789.88
|
||||||
|
|
||||||
|
Float.parse "123.456.789,88" format="#,###.##" locale=Locale.italy . should_fail_with Illegal_Argument
|
||||||
|
Float.parse "123,456,789.88" format="#.###,##" . should_fail_with Illegal_Argument
|
||||||
|
|
||||||
|
Float.parse "123,456,789.87654" locale=Locale.italy . should_equal 123.456
|
||||||
|
Float.parse "123.456.789,87654" locale=Locale.us . should_equal 123.456
|
||||||
|
|
||||||
|
Float.parse "123,456,789.654" format="###,###.##;-###,###.##" . should_equal 123456789.654
|
||||||
|
Float.parse "-123,456,789.654" format="###,###.##;-###,###.##" . should_equal -123456789.654
|
||||||
|
Float.parse "(123,456,789.654)" format="###,###.##;-###,###.##" . should_fail_with Number_Parse_Error
|
||||||
|
Float.parse "123,456,789.654" format="###,###.##;(###,###.##)" . should_equal 123456789.654
|
||||||
|
Float.parse "-123,456,789.654" format="###,###.##;(###,###.##)" . should_fail_with Number_Parse_Error
|
||||||
|
Float.parse "(123,456,789.654)" format="###,###.##;(###,###.##)" . should_equal -123456789.654
|
||||||
|
|
||||||
group_builder.specify "Float should parse hundred factorial well" <|
|
group_builder.specify "Float should parse hundred factorial well" <|
|
||||||
txt = hundred_factorial.to_text + ".345"
|
txt = hundred_factorial.to_text + ".345"
|
||||||
float = Float.parse txt
|
float = Float.parse txt
|
||||||
|
Loading…
Reference in New Issue
Block a user