to Int() - > Option<int>- Module
- std.
decimal - Receiver
- Decimal
- Minimum arity
- 0
- Maximum arity
- 0
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· 1133 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 62 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1133 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 484 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 302
Receiver
Decimal
Parameter constraints
int.toDecimal converts every int value exactly and produces scale zero. Both signed 64 bit endpoints are representable. The conversion does not pass through float.
Decimal.toInt returns Some containing the same integer when the Decimal is integral and fits the integer conversion bounds.
A nonintegral Decimal or an integer outside the int range returns None(). Conversion does not round, truncate, wrap, saturate, or raise an arithmetic overflow fault. The original Decimal remains unchanged.
A value parsed from 1.000 is integral after normalization and converts to Some containing 1. A value parsed from 1.001 is nonintegral and returns None().
Existing int, float and BigInt values do not implicitly become Decimal values in arithmetic or comparisons. The module provides no direct float or BigInt conversion member.
For an exact BigInt conversion, format it with radix 10 and pass that text to Decimal.parse. To request an integral BigInt from a Decimal, pass its canonical text to BigInt.parse with radix 10. Fractional decimal text is rejected by that integer parser.
Converting through explicitly chosen text follows the corresponding text parser. Decimal.parse never recovers decimal digits that were lost in an earlier binary float calculation.
Related rule: Convert numeric values explicitly and without truncationResult
The declared result type is Option<int>.
Effects
Failures
Result and effects
LinkReturn Some for an integral value from -9223372036854775808 through 9223372036854775807. Return None() for a fraction or a value outside that range without changing the receiver.
Why
LinkChecked narrowing preserves the value instead of choosing a rounding or overflow policy implicitly.
Resolved contracts
LinkRelated entries
LinkConvert numeric values explicitly and without truncation
LinkConvert numeric values explicitly and without truncation
int.toDecimal converts every int value exactly and produces scale zero. Both signed 64 bit endpoints are representable. The conversion does not pass through float.
Decimal.toInt returns Some containing the same integer when the Decimal is integral and fits the integer conversion bounds.
A nonintegral Decimal or an integer outside the int range returns None(). Conversion does not round, truncate, wrap, saturate, or raise an arithmetic overflow fault. The original Decimal remains unchanged.
A value parsed from 1.000 is integral after normalization and converts to Some containing 1. A value parsed from 1.001 is nonintegral and returns None().
Existing int, float and BigInt values do not implicitly become Decimal values in arithmetic or comparisons. The module provides no direct float or BigInt conversion member.
For an exact BigInt conversion, format it with radix 10 and pass that text to Decimal.parse. To request an integral BigInt from a Decimal, pass its canonical text to BigInt.parse with radix 10. Fractional decimal text is rejected by that integer parser.
Converting through explicitly chosen text follows the corresponding text parser. Decimal.parse never recovers decimal digits that were lost in an earlier binary float calculation.
Why
Exact integer conversion and checked narrowing communicate different intentions. Option exposes fractions and overflow without selecting a rounding policy for the caller. Explicit text composition handles arbitrary precision integer exchange through the existing parsers without adding another conversion API.
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 103 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 170 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 196 -
topaz.stdlib.contract.decimal-conversionatworking-tree - topaz.decision.decimal-conversion.1 at
topaz.decisi
Convert both int endpoints and reject fractions or overflow
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 (["-9223372036854775808", "9223372036854775807", "-9223372036854775809", "9223372036854775808", "1.000", "1.001", "-1.001", "-0.000"]) {let value: Decimal = read(text)match (value.toInt()) {case Some(integer) => io.writeLine("{integer}")case None() => io.writeLine("not an int")}}let large: Decimal = read("1000000000000000000000000000000")let narrowed: Option<int> = large.toInt()test.assertNone(narrowed)io.writeLine(large.toString())
-9223372036854775808 9223372036854775807 not an int not an int 1 not an int not an int 0 1000000000000000000000000000000
Expected result
Expected result
Execution completes. Exit code 0.
-92233720368547758089223372036854775807not an intnot an int1not an intnot an int01000000000000000000000000000000
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 103 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 170 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 196 -
topaz.stdlib.contract.decimal-conversionatworking-tree - topaz.decision.decimal-conversion.1 at
topaz.decisi
Convert int values to exact scale zero decimals
Linkimport std.decimal { Decimal }import std.testimport std.iolet integers: Array<int> = [-9223372036854775807 - 1, -1, 0, 1, 9223372036854775807]for integer in (integers) {let value: Decimal = integer.toDecimal()let narrowed: Option<int> = value.toInt()let recovered: int = test.assertSome(narrowed)test.assertEq(recovered, integer)io.writeLine("{value.toString()} {value.scale()}")}
-9223372036854775808 0 -1 0 0 0 1 0 9223372036854775807 0
Expected result
Expected result
Execution completes. Exit code 0.
-9223372036854775808 0-1 00 01 09223372036854775807 0
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 103 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 170 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 196 -
topaz.stdlib.contract.decimal-conversionatworking-tree - topaz.decision.decimal-conversion.1 at
topaz.decisi
Exchange arbitrary precision integers through explicit decimal text
Linkimport std.decimal { Decimal }import std.bigint { BigInt }import std.testimport std.iolet parsedInteger: Option<BigInt> = BigInt.parse("123456789012345678901234567890", 10)let integer: BigInt = test.assertSome(parsedInteger)let parsedDecimal: Option<Decimal> = Decimal.parse(integer.toString(10))let value: Decimal = test.assertSome(parsedDecimal)io.writeLine(value.toString())let parsedAgain: Option<BigInt> = BigInt.parse(value.toString(), 10)let recovered: BigInt = test.assertSome(parsedAgain)let equal = recovered == integerio.writeLine("{equal}")let parsedFraction: Option<Decimal> = Decimal.parse("1.5")let fraction: Decimal = test.assertSome(parsedFraction)let integral: Option<BigInt> = BigInt.parse(fraction.toString(), 10)match (integral) {case None() => io.writeLine("fraction rejected")case Some(_) => io.writeLine("converted")}
123456789012345678901234567890 true fraction rejected
Expected result
Expected result
Execution completes. Exit code 0.
123456789012345678901234567890truefraction rejected
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 103 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 170 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 196 -
topaz.stdlib.contract.decimal-conversionatworking-tree - topaz.decision.decimal-conversion.1 at
topaz.decisi