scale() - > 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
매개변수 제약
A Decimal represents a signed integer coefficient multiplied by ten raised to the negative scale. The coefficient has arbitrary precision. The scale is an integer from 0 through 4294967295 inclusive.
Decimal has no fixed significant digit limit or ambient precision context. Increasing the coefficient beyond the int range does not cause numeric overflow. Decimal values are finite and have no infinity or NaN.
Every Decimal is canonical. Remove trailing coefficient zeros while its scale is positive. Zero has coefficient zero and scale zero. A negative zero spelling produces the same value as zero.
Decimal.scale returns the canonical scale as int. It counts fractional decimal places after normalization. It does not retain input padding or a requested display precision.
Decimal.toString returns exact ASCII decimal text without an exponent or locale rules. A negative nonzero value starts with a minus sign. The integer part has at least one digit and no redundant leading zeros.
A positive scale produces a decimal point followed by exactly that many fractional digits. There are no trailing fractional zeros. Scale zero omits the decimal point, and zero is written as 0.
Exact addition and subtraction align the operand scales before combining their coefficients. Exact multiplication multiplies their coefficients and adds their scales without rounding.
Normalize the exact product before enforcing the scale limit. Raise TPZ4004 when its canonical scale exceeds 4294967295. An intermediate scale sum must not reject a product whose canonical scale fits.
Parsing the output of Decimal.toString recovers the same Decimal. A request for more fractional places through round or div does not append stored zeros.
Decimal values are immutable. Receiver and positional argument expressions evaluate once in source order. An earlier fault prevents later evaluation. The decimal operations themselves do not perform external effects.
관련 규칙 · Preserve exact decimal values and canonical scale →Decimal equality compares exact numeric values. Values parsed from 1.2, 1.20 and 1.200 are equal. Redundant scale in an input spelling is not part of value identity.
The equality operators == and != return complementary results. Zero and every accepted signed or padded zero spelling compare equal.
The ordering operators <, <=, > and >= use exact numeric order. They align decimal places without first rounding either operand or converting either operand to float.
Every pair of Decimal values is comparable. Equality agrees with ordering, and exactly one of less than, equal to or greater than holds.
Comparison leaves both operands and their canonical scales unchanged. Call round explicitly when comparison after rounding is the intended operation.
관련 규칙 · Compare exact decimal values independently of input scale →결과
The declared result type is int.
효과
실패
Result and effects
링크Return an int from 0 through 4294967295. Trailing coefficient zeros have already been removed, and every zero value has scale zero.
Why
링크Canonical scale observes numeric value state without preserving input padding.
Resolved contracts
링크Related entries
링크Preserve exact decimal values and canonical scale
링크Preserve exact decimal values and canonical scale
A Decimal represents a signed integer coefficient multiplied by ten raised to the negative scale. The coefficient has arbitrary precision. The scale is an integer from 0 through 4294967295 inclusive.
Decimal has no fixed significant digit limit or ambient precision context. Increasing the coefficient beyond the int range does not cause numeric overflow. Decimal values are finite and have no infinity or NaN.
Every Decimal is canonical. Remove trailing coefficient zeros while its scale is positive. Zero has coefficient zero and scale zero. A negative zero spelling produces the same value as zero.
Decimal.scale returns the canonical scale as int. It counts fractional decimal places after normalization. It does not retain input padding or a requested display precision.
Decimal.toString returns exact ASCII decimal text without an exponent or locale rules. A negative nonzero value starts with a minus sign. The integer part has at least one digit and no redundant leading zeros.
A positive scale produces a decimal point followed by exactly that many fractional digits. There are no trailing fractional zeros. Scale zero omits the decimal point, and zero is written as 0.
Exact addition and subtraction align the operand scales before combining their coefficients. Exact multiplication multiplies their coefficients and adds their scales without rounding.
Normalize the exact product before enforcing the scale limit. Raise TPZ4004 when its canonical scale exceeds 4294967295. An intermediate scale sum must not reject a product whose canonical scale fits.
Parsing the output of Decimal.toString recovers the same Decimal. A request for more fractional places through round or div does not append stored zeros.
Decimal values are immutable. Receiver and positional argument expressions evaluate once in source order. An earlier fault prevents later evaluation. The decimal operations themselves do not perform external effects.
이유
Exact coefficients preserve decimal quantities without a hidden precision setting. Canonical scale makes equality and text exchange independent of input padding. Display padding has a separate formatting intent and is not stored as numeric value state. Scale bounds apply to the value that the operation returns. A temporary representation has no distinct numeric intent and must not make an otherwise representable product fail.
Normalize signs and redundant decimal zeros
링크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 (["0", "-0.000", "+00012.3400", ".0050", "100.000"]) {let value: Decimal = read(text)io.writeLine("{value.toString()} {value.scale()}")let reparsed: Option<Decimal> = Decimal.parse(value.toString())let recovered: Decimal = test.assertSome(reparsed)test.assertEq(recovered, value)}
0 0
0 0
12.34 2
0.005 3
100 0
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
0 00 012.34 20.005 3100 0
Keep every digit beyond machine integer precision
링크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}let left: Decimal = read("1000000000000000000000000000000.25")let right: Decimal = read("0.75")let sum: Decimal = left + rightlet difference: Decimal = left - rightlet product: Decimal = left * rightio.writeLine(sum.toString())io.writeLine(difference.toString())io.writeLine(product.toString())io.writeLine(left.toString())
1000000000000000000000000000001
999999999999999999999999999999.5
750000000000000000000000000000.1875
1000000000000000000000000000000.25
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
1000000000000000000000000000001999999999999999999999999999999.5750000000000000000000000000000.18751000000000000000000000000000000.25
Normalize a product before checking its final scale
링크import std.decimal { Decimal }import std.testimport std.iolet parsedPlace: Option<Decimal> = Decimal.parse("0.1")let mut place: Decimal = test.assertSome(parsedPlace)let mut smallest: Decimal = (1).toDecimal()let mut step: int = 0while (step < 32) {smallest = smallest * placeif (step < 31) {place = place * place}step = step + 1}let doubled: Decimal = smallest * (2).toDecimal()let parsedHalf: Option<Decimal> = Decimal.parse("0.5")let half: Decimal = test.assertSome(parsedHalf)let recovered: Decimal = doubled * halflet equal = recovered == smallestio.writeLine("{recovered.scale()} {equal}")
4294967295 true
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
4294967295 true
Compare exact decimal values independently of input scale
링크Compare exact decimal values independently of input scale
Decimal equality compares exact numeric values. Values parsed from 1.2, 1.20 and 1.200 are equal. Redundant scale in an input spelling is not part of value identity.
The equality operators == and != return complementary results. Zero and every accepted signed or padded zero spelling compare equal.
The ordering operators <, <=, > and >= use exact numeric order. They align decimal places without first rounding either operand or converting either operand to float.
Every pair of Decimal values is comparable. Equality agrees with ordering, and exactly one of less than, equal to or greater than holds.
Comparison leaves both operands and their canonical scales unchanged. Call round explicitly when comparison after rounding is the intended operation.
이유
Numeric comparison answers whether quantities are equal or ordered. Retaining textual padding as a second value identity would mix formatting intent into arithmetic. Exact comparison also preserves distinctions beyond binary floating point precision.
Treat values with different input padding as equal
링크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}let left: Decimal = read("1.20")let right: Decimal = read("1.2000")let equal = left == rightlet unequal = left != rightlet less = left < rightlet lessOrEqual = left <= rightlet greater = left > rightlet greaterOrEqual = left >= rightio.writeLine("{equal} {unequal} {less} {lessOrEqual} {greater} {greaterOrEqual}")io.writeLine("{left.scale()} {right.scale()}")
true false false true false true
1 1
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
true false false true false true1 1
Order neighboring large values and signed zero exactly
링크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}let lower: Decimal = read("9007199254740992")let higher: Decimal = read("9007199254740993")let increasing = lower < higherlet decreasing = higher > lowerlet distinct = lower != higherlet negativeOrder = -higher < -lowerio.writeLine("{increasing} {decreasing} {distinct} {negativeOrder}")let negativeZero: Decimal = read("-0.000")let zero: Decimal = (0).toDecimal()let sameZero = negativeZero == zeroio.writeLine("{sameZero}")
true true true true
true
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
true true true truetrue