parse(text: string) - > Option<Decimal>- Module
- std.
decimal - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.decimal in language mode 5.21.
The current compiler selects std/decimal.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1907 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 62 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1907 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 253 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973
Receiver
Parameter constraints
Decimal.parse reads the complete input string. Text starts with an optional ASCII plus or minus sign. The remaining text contains ASCII digits and at most one decimal point.
Without a decimal point, at least one digit is required. With a decimal point, at least one digit must follow it and the integer digits may be absent. The strings .5 and -.5 are accepted. The strings 1. and . are rejected.
Leading integer zeros and trailing fractional zeros are accepted. Their exact numeric value is normalized by the decimal value rule. An accepted sign on zero does not survive normalization.
The input may contain at most 4294967295 fractional digits before normalization. A larger fractional digit count returns None(). The integer digit count has no fixed numeric precision limit.
Whitespace, non-ASCII digits, separators, radix prefixes, exponents, NaN and infinity spellings are rejected. Parsing does not trim, remove separators, stop at a valid prefix, or convert through a binary float.
Valid text returns Some containing the exact Decimal. Every malformed spelling returns None() without a numeric conversion fault or replacement value.
This grammar describes text data passed to Decimal.parse. Numeric literal spelling follows the language literal rules.
Related rule: Parse complete finite decimal textResult
The declared result type is Option<Decimal>.
Effects
Failures
Result and effects
LinkAccept an optional plus or minus sign, ASCII digits and an optional decimal point followed by digits. A missing integer part is accepted. Invalid text or too many fractional digits returns None().
Why
LinkComplete input parsing preserves exact decimal data and makes malformed spellings visible.
Resolved contracts
LinkRelated entries
LinkParse complete finite decimal text
LinkParse complete finite decimal text
Decimal.parse reads the complete input string. Text starts with an optional ASCII plus or minus sign. The remaining text contains ASCII digits and at most one decimal point.
Without a decimal point, at least one digit is required. With a decimal point, at least one digit must follow it and the integer digits may be absent. The strings .5 and -.5 are accepted. The strings 1. and . are rejected.
Leading integer zeros and trailing fractional zeros are accepted. Their exact numeric value is normalized by the decimal value rule. An accepted sign on zero does not survive normalization.
The input may contain at most 4294967295 fractional digits before normalization. A larger fractional digit count returns None(). The integer digit count has no fixed numeric precision limit.
Whitespace, non-ASCII digits, separators, radix prefixes, exponents, NaN and infinity spellings are rejected. Parsing does not trim, remove separators, stop at a valid prefix, or convert through a binary float.
Valid text returns Some containing the exact Decimal. Every malformed spelling returns None() without a numeric conversion fault or replacement value.
This grammar describes text data passed to Decimal.parse. Numeric literal spelling follows the language literal rules.
Why
A complete decimal data grammar makes accepted input predictable and preserves every digit. Optional signs and a missing integer zero are accepted input conventions. Canonical output still gives each value one serialized spelling.
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 24 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 175 -
topaz.stdlib.contract.decimal-parseatworking-tree - topaz.decision.decimal-parse.1 at
topaz.decisi
Accept explicit signs and fractional decimal text
Linkimport std.decimal { Decimal }import std.testimport std.iofunction read(text: string) -> Decimal {let parsed: Option<Decimal> = Decimal.parse(text)let value: Decimal = test.assertSome(parsed)return value}for text in (["7", "+7", "-7", "0007", ".5", "-.5", "+.5", "01.0200"]) {let value: Decimal = read(text)io.writeLine(value.toString())}
7 7 -7 7 0.5 -0.5 0.5 1.02
Expected result
Expected result
Execution completes. Exit code 0.
77-770.5-0.50.51.02
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 24 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 175 -
topaz.stdlib.contract.decimal-parseatworking-tree - topaz.decision.decimal-parse.1 at
topaz.decisi
Reject incomplete and nondecimal spellings
Linkimport std.decimal { Decimal }import std.iofor text in (["", "+", "-", ".", "+.", "-.", "1.", "1.2.3", "1e2", "1E+2", "0x10", "1_000", "1,2", "NaN", "Infinity", " 1", "1 ", "1.2", "١.٢", "1\n2", "\t1"]) {match (Decimal.parse(text)) {case None() => io.writeLine("rejected")case Some(_) => io.writeLine("accepted")}}
rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected
Expected result
Expected result
Execution completes. Exit code 0.
rejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejected
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 24 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 175 -
topaz.stdlib.contract.decimal-parseatworking-tree - topaz.decision.decimal-parse.1 at
topaz.decisi