Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека · std.decimal

    Decimal.parse

    Parse complete signed ASCII decimal text into an exact optional Decimal.

    parse(text: string) -> Option<Decimal>
    Модуль
    std.decimal →
    Получатель
    Неприменимо
    Минимальное число аргументов
    1
    Максимальное число аргументов
    1

    Declaration and parameters

    Ссылка

    Доступность

    Включено по умолчанию · Не определено

    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.

    Получатель

    Ограничения параметров

    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.

    Результат

    The declared result type is Option<Decimal>.

    Эффекты

    Ошибки

    Result and effects

    Ссылка

    Accept 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().

    Complete input parsing preserves exact decimal data and makes malformed spellings visible.

    Resolved contracts

    Ссылка

    Related entries

    Ссылка

    std.decimal →

    Parse complete finite decimal text

    Ссылка
    Правилоtopaz.stdlib.contract.decimal-parse.rule

    Parse 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.

    Причина

    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.

    Accept explicit signs and fractional decimal text

    Ссылка
    main.tpzAccept explicit signs and fractional decimal textОткрыть песочницу →
    1. import std.decimal { Decimal }
    2. import std.test
    3. import std.io
    4. function read(text: string) -> Decimal {
    5. let parsed: Option<Decimal> = Decimal.parse(text)
    6. let value: Decimal = test.assertSome(parsed)
    7. return value
    8. }
    9. for text in (["7", "+7", "-7", "0007", ".5", "-.5", "+.5", "01.0200"]) {
    10. let value: Decimal = read(text)
    11. io.writeLine(value.toString())
    12. }
    Ожидаемый выводКод завершения 0
    7
    7
    -7
    7
    0.5
    -0.5
    0.5
    1.02
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. 7
    2. 7
    3. -7
    4. 7
    5. 0.5
    6. -0.5
    7. 0.5
    8. 1.02

    Reject incomplete and nondecimal spellings

    Ссылка
    main.tpzReject incomplete and nondecimal spellingsОткрыть песочницу →
    1. import std.decimal { Decimal }
    2. import std.io
    3. for text in (["", "+", "-", ".", "+.", "-.", "1.", "1.2.3", "1e2", "1E+2", "0x10", "1_000", "1,2", "NaN", "Infinity", " 1", "1 ", "1.2", "١.٢", "1\n2", "\t1"]) {
    4. match (Decimal.parse(text)) {
    5. case None() => io.writeLine("rejected")
    6. case Some(_) => io.writeLine("accepted")
    7. }
    8. }
    Ожидаемый выводКод завершения 0
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. rejected
    2. rejected
    3. rejected
    4. rejected
    5. rejected
    6. rejected
    7. rejected
    8. rejected
    9. rejected
    10. rejected
    11. rejected
    12. rejected
    13. rejected
    14. rejected
    15. rejected
    16. rejected
    17. rejected
    18. rejected
    19. rejected
    20. rejected
    21. rejected

    Меню

    Загрузка →