static class · System
Number
public static class Number
Numbers as text and back.
- Tipo
- static class
- Espacio de nombres
- System
- Ensamblado
- System.Core
- Acceso
- public, static, abstract
- Archivo fuente
Primitives.ant
Herencia
- Clases base
-
AnyNumber
Métodos
The decimal digits of value (with a '-' when negative).
The decimal digits of value.
The decimal digits of a 128-bit value: 19 digits at a time (one division by 10^19 for each, the digits
of each part from a u64).
The decimal digits of a 256-bit value: 19 digits at a time, as for 128 bits.
How many decimal digits value has (1 for 0).
Writes the digits of value so they end just before end.
The integer text writes (an optional '-', then decimal digits); a FormatError otherwise.
The integer text writes (an optional '-', then decimal digits), or null.
Miembros privados e internos (1)
Miembros heredados (5)
GetTypemétodo de AnyUndescribedmétodo de AnyToStringmétodo de AnyHashmétodo de Anydestroymétodo de Any
Código fuente
Number en Antril, descompilado a partir de su Larva HIR.
// System.Number — System.Core (decompiled from Larva HIR)
into namespace System
{
/// Numbers as text and back.
public static class Number
{
/// The digits of `top`, then 19 for each of parts[count - 1] … parts[0] (with their leading zeros).
Chunked(u64 top, u64[] parts, i32 count) : str
{
i32 lead = DigitCount(top);
u8[] digits = new u8[lead + 19 * count];
WriteDigits(top, digits, lead);
loop (i32 i = 0..count)
{
u64 part = parts[count - 1 - i];
i32 at = lead + 19 * (i + 1);
loop (i32 j = 0..19)
{
at--;
digits[at] = (u8)(48 + part % 10);
part = part / 10;
}
}
return new str(digits);
}
public:
/// The decimal digits of `value` (with a '-' when negative).
Format(i64 value) : str
{
if (value >= 0)
return FormatUnsigned((u64)value);
u64 magnitude = (u64)-(value + 1) + 1;
return String.Decimal(magnitude, true);
}
/// The decimal digits of `value`.
FormatUnsigned(u64 value) : str => String.Decimal(value, false);
/// The decimal digits of a 128-bit `value`: 19 digits at a time (one division by 10^19 for each, the digits
/// of each part from a u64).
FormatUnsigned(u128 value) : str
{
if (value >> 64 == 0)
return FormatUnsigned((u64)value);
u64[] parts = new u64[2];
i32 count = 0;
u128 chunk = 10000000000000000000;
loop (value >= chunk)
{
u128 rest = value / chunk;
parts[count] = (u64)(value - rest * chunk);
count++;
value = rest;
}
return Chunked((u64)value, parts, count);
}
/// The decimal digits of a 256-bit `value`: 19 digits at a time, as for 128 bits.
FormatUnsigned(u256 value) : str
{
if (value >> 64 == 0)
return FormatUnsigned((u64)value);
u64[] parts = new u64[4];
i32 count = 0;
u256 chunk = 10000000000000000000;
loop (value >= chunk)
{
u256 rest = value / chunk;
parts[count] = (u64)(value - rest * chunk);
count++;
value = rest;
}
return Chunked((u64)value, parts, count);
}
/// How many decimal digits `value` has (1 for 0).
DigitCount(u64 value) : i32
{
i32 count = 1;
loop (value >= 10)
{
value = value / 10;
count++;
}
return count;
}
/// Writes the digits of `value` so they end just before `end`.
WriteDigits(u64 value, u8[] units, i32 end)
{
i32 at = end;
do
{
at--;
units[at] = (u8)(48 + value % 10);
value = value / 10;
}
loop (value > 0);
}
/// The integer `text` writes (an optional '-', then decimal digits); a FormatError otherwise.
Parse(str text) : i64
{
i64? value = TryParse(text);
if (value == null)
throw new FormatError("not a number: " + text);
return value.Value;
}
/// The integer `text` writes (an optional '-', then decimal digits), or null.
TryParse(str text) : i64?
{
i32 length = text.Length;
bool negative = length > 0 && text[0] == 45;
i32 start = negative ? 1 : 0;
if (start == length)
return null;
i64 value = 0;
loop (i32 i = start..length)
{
u8 unit = text[i];
if (unit < 48 || unit > 57)
return null;
i64 digit = (i64)unit - 48;
if (value < (-9223372036854775808 + digit) / 10)
return null;
value = value * 10 - digit;
}
if (!negative && value == -9223372036854775808)
return null;
return negative ? value : -value;
}
}
}