to Int() - > Option<int>- 수신자
- Decimal
- 최소 인자 수
- 0
- 최대 인자 수
- 0
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
매개변수 제약
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.
관련 규칙 · Convert numeric values explicitly and without truncation →결과
The declared result type is Option<int>.
효과
실패
Result and effects
링크Return 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
링크Checked narrowing preserves the value instead of choosing a rounding or overflow policy implicitly.
Resolved contracts
링크Related entries
링크Convert numeric values explicitly and without truncation
링크Convert 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.
이유
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.
Convert both int endpoints and reject fractions or overflow
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
-92233720368547758089223372036854775807not an intnot an int1not an intnot an int01000000000000000000000000000000
Convert int values to exact scale zero decimals
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
-9223372036854775808 0-1 00 01 09223372036854775807 0
Exchange arbitrary precision integers through explicit decimal text
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
123456789012345678901234567890truefraction rejected