Skip to content

Commit 3a22147

Browse files
Add format parameter to Decimal.parse (#11205)
1 parent 0b8a0c4 commit 3a22147

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
- [Added .floor, .ceil, .trunc to the in-memory `Decimal` column.][10887]
6565
- [Added vectorized .round to the in-memory `Decimal` column.][10912]
6666
- [`select_into_database_table` no longer defaults the primary key to the first
67-
column][11120]
67+
column.][11120]
68+
- [Added `format` parameter to `Decimal.parse`.][11205]
6869

6970
[10614]: https://github.com/enso-org/enso/pull/10614
7071
[10660]: https://github.com/enso-org/enso/pull/10660
@@ -77,6 +78,7 @@
7778
[10887]: https://github.com/enso-org/enso/pull/10887
7879
[10912]: https://github.com/enso-org/enso/pull/10912
7980
[11120]: https://github.com/enso-org/enso/pull/11120
81+
[11205]: https://github.com/enso-org/enso/pull/11205
8082

8183
#### Enso Language & Runtime
8284

distribution/lib/Standard/Base/0.0.0-dev/src/Data/Decimal.enso

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,10 +1116,13 @@ type Decimal
11161116
Arguments:
11171117
- text: The text to parse into a `Decimal`.
11181118
- locale: The locale that specifies the format to use when parsing.
1119+
- format: The Java-style format to use to parse the string.
11191120

11201121
! Error Conditions
11211122

11221123
- If `text` is incorrectly formatted, a `Number_Parse_Error` is thrown.
1124+
- If `format` is incorrectly formatted (or does not match the `locale`,
1125+
an `Illegal_Argument` is thrown.
11231126

11241127
> Example
11251128
Parse a `Decimal` with no local specifier.
@@ -1144,11 +1147,21 @@ type Decimal
11441147

11451148
Decimal.parse "123.456.789,87654" locale=Locale.italy
11461149
# => 123456789.87654
1147-
parse : Text -> Locale | Nothing -> Decimal ! Number_Parse_Error
1148-
parse text locale:(Locale | Nothing)=Nothing -> Decimal ! Number_Parse_Error = case locale of
1149-
Nothing -> Decimal.from_text text
1150-
Locale.Value java_locale -> Panic.catch ParseException ((NumberFormat.getInstance java_locale).parse text) _->
1151-
Error.throw (Number_Parse_Error.Error text)
1150+
1151+
> Example
1152+
Parse a `Decimal` with an explicit negative number format.
1153+
1154+
Decimal.parse "(123,456,789.654)" format="###,###.##;(###,###.##)"
1155+
# => -123456789.654
1156+
parse : Text -> Locale | Nothing -> Decimal ! Number_Parse_Error ! Illegal_Argument
1157+
parse text locale:Locale=Locale.default format:Text="" -> Decimal ! Number_Parse_Error ! Illegal_Argument =
1158+
Illegal_Argument.handle_java_exception <|
1159+
# `getInstance` returns `DecimalFormat` or a subclass of `DecimalFormat`.
1160+
decimal_format = NumberFormat.getInstance locale.java_locale
1161+
decimal_format.applyLocalizedPattern format
1162+
decimal_format.setParseBigDecimal True
1163+
Panic.catch ParseException (Decimal.Value (decimal_format.parse text)) _->
1164+
Error.throw (Number_Parse_Error.Error text)
11521165

11531166
## PRIVATE
11541167
precision : Integer

test/Base_Tests/src/Data/Decimal_Spec.enso

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -798,14 +798,26 @@ add_specs suite_builder =
798798
d.format format="#,##.##" locale=Locale.italy . should_equal "1.23.45.67.89,88"
799799

800800
group_builder.specify "should parse correctly" <|
801-
Decimal.parse "123456789.87654" . should_equal 123456789.87654
802-
Decimal.parse "123,456,789.87654" locale=Locale.default . should_equal 123456789.87654
803-
Decimal.parse "123,456,789.87654" locale=Locale.us . should_equal 123456789.87654
804-
Decimal.parse "123.456.789,87654" locale=Locale.italy . should_equal 123456789.87654
801+
Decimal.parse "123,456,789.87654" . should_equal (dec "123456789.87654")
802+
Decimal.parse "123.456.789,87654" locale=Locale.italy . should_equal (dec "123456789.87654")
803+
804+
Decimal.parse "123,456,789.88" format="#,###.##" . should_equal (dec "123456789.88")
805+
Decimal.parse "123.456.789,88" format="#.###,##" locale=Locale.italy . should_equal (dec "123456789.88")
806+
Decimal.parse "1,23,45,67,89.88" format="#,##.##" . should_equal (dec "123456789.88")
807+
Decimal.parse "1.23.45.67.89,88" format="#.##,##" locale=Locale.italy . should_equal (dec "123456789.88")
808+
809+
Decimal.parse "123.456.789,88" format="#,###.##" locale=Locale.italy . should_fail_with Illegal_Argument
810+
Decimal.parse "123,456,789.88" format="#.###,##" . should_fail_with Illegal_Argument
811+
805812
Decimal.parse "123,456,789.87654" locale=Locale.italy . should_equal 123.456
806813
Decimal.parse "123.456.789,87654" locale=Locale.us . should_equal 123.456
807814

808-
Decimal.parse "123,456,789.87654" . should_fail_with Number_Parse_Error
815+
Decimal.parse "123,456,789.654" format="###,###.##;-###,###.##" . should_equal 123456789.654
816+
Decimal.parse "-123,456,789.654" format="###,###.##;-###,###.##" . should_equal -123456789.654
817+
Decimal.parse "(123,456,789.654)" format="###,###.##;-###,###.##" . should_fail_with Number_Parse_Error
818+
Decimal.parse "123,456,789.654" format="###,###.##;(###,###.##)" . should_equal 123456789.654
819+
Decimal.parse "-123,456,789.654" format="###,###.##;(###,###.##)" . should_fail_with Number_Parse_Error
820+
Decimal.parse "(123,456,789.654)" format="###,###.##;(###,###.##)" . should_equal -123456789.654
809821

810822
suite_builder.group "(Decimal_Spec) signs" group_builder->
811823
group_builder.specify "should calculate abs correctly" <|

0 commit comments

Comments
 (0)