Skip to content

CkNumber

Job: turn whatever a database, a payload or a form hands you into the PHP number you ask for — an int, a float, an exact decimal as text — knowing it fits the column it is going into, without ever throwing.

Values arrive in all sorts of wrappers: an int from a driver, a float from a decoded JSON payload, the text '42' from a form field, the text '12.50' a DECIMAL column travels in. CkNumber takes any of them — the input parameter is mixed, so you can pass a number of either PHP type or plain text — and gives you back the shape you named. When the value is missing, not a number, or too big for the type, you get the fallback instead — 0 unless you pick another. That is this class's standard rejection value: the fallback, never an exception.

The readers are named for the database column a value is bound for. TinyInt, SmallInt, MediumInt, Integer and BigInt read an int that fits the column of that name — unsigned unless told otherwise — so a value is known to fit before the database is asked. There is no plain Int, on purpose: a whole number is always judged by the column that will hold it, and a PHP int with no column in mind is BigInt with unsigned: false. Float reads PHP's float; Decimal reads an exact decimal, every digit kept, as the plain text a DECIMAL column arrives in and is saved from. In a reason or a message a type goes by a fixed name — Int32 for a signed INT, Int64 for a signed BIGINT, Double for a float, Byte for an unsigned TINYINT — so the words are the same whatever wrote them.

Text is read the plain, invariant way: an optional sign, digits, a dot for the decimal point, an optional exponent ('4.2e1' is 42). Grouping commas and currency symbols are not numbers here — '1,234' and '$5' are rejected. Money-dressed text is CkMoney's job.

Quick look

use SafeCoder\CommonKit\CkNumber;

CkNumber::Integer('42');        // -> 42
CkNumber::Integer('1e3');       // -> 1000   (exponent form is fine)
CkNumber::Decimal('12.50');     // -> '12.5' (every digit kept, as text — ready for a DECIMAL column)
CkNumber::Integer('$5');        // -> 0      (money text — rejected; that's CkMoney's job)
CkNumber::IntegerOrNull(null);  // -> null   (missing stays missing)
CkNumber::TinyInt('300');       // -> 0      (outside 0 to 255 — the fallback)
CkNumber::IsBetween(
    input: '5',
    min: 1,
    max: 10
);                              // -> true
CkNumber::IsPositive(
    input: 0,
    allowZero: true
);                              // -> true
CkNumber::Clamp(
    input: '250',
    min: 1,
    max: 100
);                              // -> 100
CkNumber::Divide(
    numerator: 5,
    denominator: 0,
    fallback: -1.0
);                              // -> -1.0   (no crash)
CkNumber::Explain('1,234');     // -> 'not a number'

Methods

The first eight methods are one reader per PHP number type and per database column. They all work the same way; you just pick the size and shape you need — the shared rules live here, each method's own type and range in its section below.

A value outside the type's range is rejected — never silently wrapped or clipped. So is anything that simply is not a number: a boolean, an array, NAN, an infinity, nothing at all.

Rounding. The five column readers round a fractional value to the nearest whole number. Halves go to the even neighbour — this is called banker's rounding, and it stops thousands of .5 values from all drifting upward. If you would rather refuse fractions than round them, pass exact: true.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Integer(12.5);   // -> 12    (half rounds to the even neighbour)
CkNumber::Integer(13.5);   // -> 14    (so does this one)

CkNumber::Integer(
    input: '12.5',
    exact: true,
    fallback: 42
);                         // -> 42    (exact: true refuses fractions)

Float and Decimal have no exact parameter — fractions are their normal food, so there is nothing to round or refuse.

The fallback. Every reader takes a fallback — the value that comes back when the input cannot be read. It is already the right type, so it comes back exactly as you gave it, and it defaults to 0 ('0' for Decimal). Pick a fallback that your code can tell apart from a real value when that matters. The OrNull twins take no fallback — their answer for anything unusable is null — and neither do the validators, whose answer is false.

Float

The everyday number with a decimal point.

static function Float(
    mixed $input = null,
    float $fallback = 0.0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): float
Parameter Type Default What it does
input mixed null The value: a number, or text.
fallback float 0.0 What comes back when the value cannot be converted.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number the type holds The converted value: 2.5 The converted value: 2.5
Rejected: missing, not a number (NAN and the infinities included), or beyond a float's range ('1e400') The fallback (default 0.0) Throws CkNumberException — carries Input, TargetType and Reason

You get a float — about 15 significant digits. This is the type a decoded JSON number with a fraction arrives as. In a reason the type is Double.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Float('2.5');    // -> 2.5
CkNumber::Float('1e400');  // -> 0.0  (more than a float can hold — the fallback)

TinyInt

A tiny whole number for a TINYINT column.

static function TinyInt(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    int $fallback = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column, which holds no negative number; false for a signed one — and always false for PostgreSQL, which has no unsigned columns.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers the fallback.
fallback int 0 What comes back when the value cannot be converted.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number the column holds The converted value: 42 The converted value: 42
Rejected: missing, not a number, outside 0 to 255 (-128 to 127 signed), or a fraction with exact: true The fallback (default 0) Throws CkNumberException — carries Input, TargetType and Reason

You get an int that fits a TINYINT — one byte: 0 to 255, or -128 to 127 with unsigned: false. Read as every whole-number reader here reads, and a value the column cannot hold answers the fallback, never a wrapped or truncated number — so what is about to be saved is known to fit before the database is asked. In a reason the type is Byte, SByte when signed.

use SafeCoder\CommonKit\CkNumber;

CkNumber::TinyInt('42');    // -> 42
CkNumber::TinyInt('300');   // -> 0     (too big for the column — the fallback)
CkNumber::TinyInt('-100');  // -> 0     (negative, and the column is unsigned by default)

CkNumber::TinyInt(
    input: '-100',
    unsigned: false
);   // -> -100  (a signed column has room for it)

SmallInt

A small whole number for a SMALLINT column.

static function SmallInt(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    int $fallback = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column, which holds no negative number; false for a signed one — and always false for PostgreSQL, which has no unsigned columns.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers the fallback.
fallback int 0 What comes back when the value cannot be converted.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number the column holds The converted value: 65535 The converted value: 65535
Rejected: missing, not a number, outside 0 to 65,535 (-32,768 to 32,767 signed), or a fraction with exact: true The fallback (default 0) Throws CkNumberException — carries Input, TargetType and Reason

You get an int that fits a SMALLINT — two bytes: 0 to 65,535, or -32,768 to 32,767 with unsigned: false. Works like TinyInt, with more room. In a reason the type is UInt16, Int16 when signed.

use SafeCoder\CommonKit\CkNumber;

CkNumber::SmallInt('65535');   // -> 65535
CkNumber::SmallInt('-1');      // -> 0     (negative — the fallback)

CkNumber::SmallInt(
    input: '32000',
    unsigned: false
);   // -> 32000

MediumInt

A medium whole number for a MEDIUMINT column.

static function MediumInt(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    int $fallback = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column, which holds no negative number; false for a signed one.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers the fallback.
fallback int 0 What comes back when the value cannot be converted.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number the column holds The converted value: 16777215 The converted value: 16777215
Rejected: missing, not a number, outside 0 to 16,777,215 (-8,388,608 to 8,388,607 signed), or a fraction with exact: true The fallback (default 0) Throws CkNumberException — carries Input, TargetType and Reason

You get an int that fits a MySQL MEDIUMINT — three bytes: 0 to 16,777,215, or -8,388,608 to 8,388,607 with unsigned: false. In a reason the type is UInt24, Int24 when signed.

use SafeCoder\CommonKit\CkNumber;

CkNumber::MediumInt('16777215');   // -> 16777215
CkNumber::MediumInt('16777216');   // -> 0     (one too many — the fallback)

Integer

The everyday whole number for an INT column.

static function Integer(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    int $fallback = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column, which holds no negative number; false for a signed one — and always false for PostgreSQL, which has no unsigned columns.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers the fallback.
fallback int 0 What comes back when the value cannot be converted.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number the column holds The converted value: 4000000000 The converted value: 4000000000
Rejected: missing, not a number, outside 0 to about 4.3 billion (about ±2.1 billion signed), or a fraction with exact: true The fallback (default 0) Throws CkNumberException — carries Input, TargetType and Reason

You get an int that fits an INT — INTEGER in full — four bytes: 0 to 4,294,967,295, or -2,147,483,648 to 2,147,483,647 with unsigned: false. This is the column most ids and counts live in. Not BigInt: that one is eight bytes wide and lets 3,000,000,000 through — more than a signed INT column holds. For an INT column, this reader; there is no plain Int here on purpose, so the choice is always made. In a reason the type is UInt32, Int32 when signed.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Integer('4000000000');   // -> 4000000000  (fits an unsigned INT)
CkNumber::Integer('-1');           // -> 0     (negative — the fallback)

CkNumber::Integer(
    input: '3000000000',
    unsigned: false
);   // -> 0  (too big for a signed INT — the fallback)

BigInt

A big whole number for a BIGINT column.

static function BigInt(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    int $fallback = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column, which holds no negative number; false for a signed one — and always false for PostgreSQL, which has no unsigned columns.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers the fallback.
fallback int 0 What comes back when the value cannot be converted.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number the reader holds The converted value: 9000000000 The converted value: 9000000000
Rejected: missing, not a number, outside 0 to 9,223,372,036,854,775,807 (about ±9.2 quintillion signed), or a fraction with exact: true The fallback (default 0) Throws CkNumberException — carries Input, TargetType and Reason

You get an int that fits a BIGINT — eight bytes. Signed, it is the full range of a PHP int, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, about ±9.2 quintillion: the reader for a whole number with no narrower column to fit, since there is no plain Int here. Unsigned, the column goes up to 18,446,744,073,709,551,615 but a PHP int stops at 9,223,372,036,854,775,807, so that is where this reader stops too; a value in the stretch beyond is read exactly, as text, by Decimal. Text is read digit by digit, never through a float, so '9007199254740993' — one past what a float can count to — arrives as exactly that. In a reason the type is UInt64, Int64 when signed.

use SafeCoder\CommonKit\CkNumber;

CkNumber::BigInt('9000000000');             // -> 9000000000
CkNumber::BigInt('12.00');                  // -> 12    (a whole number in decimal clothes)
CkNumber::BigInt('9007199254740993');       // -> 9007199254740993   (every digit, never through a float)
CkNumber::BigInt('-1');                     // -> 0     (negative — the fallback)
CkNumber::BigInt('18446744073709551615');   // -> 0     (past a PHP int — read it with Decimal)

CkNumber::BigInt(
    input: '-1',
    unsigned: false
);   // -> -1  (a signed column — or a PHP int with no column in mind)

Decimal

The exact number for a DECIMAL column, as text.

static function Decimal(
    mixed $input = null,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The value: a number, or text.
fallback string '0' What comes back when the value cannot be converted, read the same way — so '0.00' comes back as '0'; a fallback that is not a number itself acts as '0'.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be converted.

Returns

When Default With throwIfInvalid: true
The value reads as a number The number as plain text, every digit kept: '12.5' The number as plain text: '12.5'
Rejected: missing, or not a number (NAN and the infinities included) The fallback (default '0') Throws CkNumberException — carries Input, TargetType and Reason

You get the number as text — '1234.5', '-0.025', '1000' — the shape a DECIMAL or NUMERIC column arrives in and can be saved from, since PHP has no decimal type to hand back. Every digit is kept, however many there are, where Float keeps about fifteen, and there is no ceiling. The text is the number, not its spelling: no exponent, no +, no leading zeros and no trailing ones, so '12.50' and '1.25e1' both come back as '12.5'. A float is read by its first fifteen significant digits, so 0.1 + 0.2 is the '0.3' it was meant to be. For an amount written with its currency's decimals, '12.50', see CkMoney::Format.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Decimal('12.50');                  // -> '12.5'  (the number, not its spelling)
CkNumber::Decimal('1e30');                   // -> '1000000000000000000000000000000'
CkNumber::Decimal('18446744073709551615');   // -> '18446744073709551615'  (past a PHP int: exact here)
CkNumber::Decimal(0.1 + 0.2);                // -> '0.3'

CkNumber::Decimal(
    input: 'junk',
    fallback: '0.00'
);   // -> '0'  (the fallback is read the same way)

Each reader has a twin ending in OrNull — IntegerOrNull gives an ?int, DecimalOrNull a ?string, and so on for all seven. Instead of a stand-in fallback, the twin answers null when there is nothing usable: a missing value (null), a value that cannot be read — and, by default, a zero, because in many database columns 0 means "not set". That makes these the right pick for filling a nullable column: the column receives an honest NULL, never a fake number.

When zero is a real value in your data — a count, a balance — switch that off with treatZeroAsMissing: false.

The integer twins take exact too, and the column twins unsigned; the Float and Decimal twins take neither, for the same reason as above.

These twins pair naturally with CkDictionary when you are pulling values out of a parsed payload, and with CkSentinel when you are writing to storage.

FloatOrNull

The OrNull twin of Float.

static function FloatOrNull(
    mixed $input = null,
    bool $treatZeroAsMissing = true
): ?float
Parameter Type Default What it does
input mixed null The value: a number, or text.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The converted value: 2.5
Missing (null), unreadable — not a number (NAN and the infinities included), or beyond a float's range — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get a ?float.

use SafeCoder\CommonKit\CkNumber;

CkNumber::FloatOrNull('12.5');   // -> 12.5
CkNumber::FloatOrNull(0.0);      // -> null  (zero reads as "not set" by default)

TinyIntOrNull

The OrNull twin of TinyInt.

static function TinyIntOrNull(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    bool $treatZeroAsMissing = true
): ?int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column; false for a signed one.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers null.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The converted value: 42
Missing (null), unreadable — not a number, outside 0 to 255 (-128 to 127 signed), or a fraction with exact: true — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get an ?int — TinyInt's conversion rules, null instead of a fallback.

use SafeCoder\CommonKit\CkNumber;

CkNumber::TinyIntOrNull('300');   // -> null  (too big for the column)
CkNumber::TinyIntOrNull(0);       // -> null  (zero reads as "not set" by default)

SmallIntOrNull

The OrNull twin of SmallInt.

static function SmallIntOrNull(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    bool $treatZeroAsMissing = true
): ?int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column; false for a signed one.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers null.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The converted value: 65535
Missing (null), unreadable — not a number, outside 0 to 65,535 (-32,768 to 32,767 signed), or a fraction with exact: true — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get an ?int.

use SafeCoder\CommonKit\CkNumber;

CkNumber::SmallIntOrNull('junk');   // -> null  (unreadable)

MediumIntOrNull

The OrNull twin of MediumInt.

static function MediumIntOrNull(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    bool $treatZeroAsMissing = true
): ?int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column; false for a signed one.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers null.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The converted value: 16777215
Missing (null), unreadable — not a number, outside 0 to 16,777,215 (-8,388,608 to 8,388,607 signed), or a fraction with exact: true — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get an ?int.

use SafeCoder\CommonKit\CkNumber;

CkNumber::MediumIntOrNull(0);   // -> null  (zero reads as "not set" by default)

IntegerOrNull

The OrNull twin of Integer.

static function IntegerOrNull(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    bool $treatZeroAsMissing = true
): ?int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column; false for a signed one.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers null.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The converted value: 4000000000
Missing (null), unreadable — not a number, outside 0 to about 4.3 billion (about ±2.1 billion signed), or a fraction with exact: true — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get an ?int.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IntegerOrNull('4000000000');   // -> 4000000000
CkNumber::IntegerOrNull('-1');           // -> null  (negative — nothing usable)

BigIntOrNull

The OrNull twin of BigInt.

static function BigIntOrNull(
    mixed $input = null,
    bool $unsigned = true,
    bool $exact = false,
    bool $treatZeroAsMissing = true
): ?int
Parameter Type Default What it does
input mixed null The value: a number, or text.
unsigned bool true true for an UNSIGNED column; false for a signed one.
exact bool false Rounds a fractional value to the nearest whole number first, halves to the even neighbour; pass true to accept only whole values, so 12.5 answers null.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The converted value: 42
Missing (null), unreadable — not a number, outside 0 to 9,223,372,036,854,775,807 (about ±9.2 quintillion signed), or a fraction with exact: true — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get an ?int.

use SafeCoder\CommonKit\CkNumber;

CkNumber::BigIntOrNull('42');   // -> 42

DecimalOrNull

The OrNull twin of Decimal.

static function DecimalOrNull(
    mixed $input = null,
    bool $treatZeroAsMissing = true
): ?string
Parameter Type Default What it does
input mixed null The value: a number, or text.
treatZeroAsMissing bool true Reads a zero as missing, so it comes back as null too; pass false to keep a zero as the real value it sometimes is.

Returns

When You get
The value converts to a usable number The number as plain text: '19.99'
Missing (null), unreadable — not a number (NAN and the infinities included) — or a zero with treatZeroAsMissing on (the default) null — this method never throws

You get a ?string — the right shape for a nullable money or rate column.

use SafeCoder\CommonKit\CkNumber;

CkNumber::DecimalOrNull('19.99');   // -> '19.99'
CkNumber::DecimalOrNull('0.00');    // -> null  (zero reads as "not set" by default)

Percent

A percentage as the rate it stands for.

static function Percent(
    mixed $input = null,
    float $fallback = 0.0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): float
Parameter Type Default What it does
input mixed null The value: text with or without a trailing %, or a number.
fallback float 0.0 What comes back when the value cannot be read.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be read.

Returns

When Default With throwIfInvalid: true
The value reads as a percentage or a bare rate The rate: 0.15 The rate: 0.15
Rejected: missing, or unreadable — grouping, currency symbols, junk The fallback (default 0.0) Throws CkNumberException — carries Input, TargetType and Reason

Percent reads a percentage and answers the rate — the number you would multiply by. A trailing % (with or without a space before it) divides the value by 100. A bare value — no % sign — is taken as the rate it already is, untouched. The division is exact, and the answer is the nearest float to it.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Percent('15%');     // -> 0.15
CkNumber::Percent('2.5 %');   // -> 0.025
CkNumber::Percent('0.15');    // -> 0.15  (bare values are rates already)
CkNumber::Percent(15);        // -> 15.0  (bare 15 is the rate 15 — that is fifteen hundred percent!)

CkNumber::Percent(
    input: 'junk',
    fallback: -1.0
);                            // -> -1.0

That last pair is the big gotcha: CkNumber never guesses which scale a bare number meant. 0.15 and 15 are both perfectly honest rates, so rescaling one by guess would corrupt the other. If your form posts whole percent numbers without the sign, send the % along or divide by 100 upstream.

PercentOrNull

The OrNull twin of Percent.

static function PercentOrNull(
    mixed $input = null,
    bool $treatZeroAsMissing = true
): ?float
Parameter Type Default What it does
input mixed null The value: text with or without a trailing %, or a number.
treatZeroAsMissing bool true Reads a zero rate as missing, so it comes back as null too; pass false to keep a real 0%.

Returns

When You get
The value reads as a usable rate The rate: 0.15
Missing (null), unreadable, or a zero rate with treatZeroAsMissing on (the default) null — this method never throws

PercentOrNull is the family's OrNull twin: null for a missing value, an unreadable one, or — by default — a zero rate (treatZeroAsMissing: false keeps a real 0%).

use SafeCoder\CommonKit\CkNumber;

CkNumber::PercentOrNull('0%');   // -> null  (zero percent reads as "not set" by default)

IsNumber

Does it read as a number at all?

static function IsNumber(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A number of either type, or plain invariant text such as '42', '-1.5', '4.2e1' true true
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

The yes-or-no form of the readers, under their rules: '1,234' and '$5' are not numbers here, that is CkMoney's job; booleans, arrays, NAN and the infinities are not numbers either. Zero is a number here, whatever the OrNull family says about it. When the value is going to be stored, read it with the typed OrNull reader instead and check for null.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsNumber('4.2e1');   // -> true
CkNumber::IsNumber('1,234');   // -> false  (grouping — CkMoney's job)
CkNumber::IsNumber(true);      // -> false

IsInteger

A whole number?

static function IsInteger(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A number with no fractional part: 12, '12.0', '1e3' true true
A number with a fraction: '12.5', 0.1 false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

The exact: true rule of the integer readers, as a verdict, without choosing a width — a value too large for a PHP int is still whole. HasDecimals with its default of 0 asks the same question.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsInteger('12.0');   // -> true
CkNumber::IsInteger('12.5');   // -> false

IsPositive

Above zero?

static function IsPositive(
    mixed $input = null,
    bool $allowZero = false,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
allowZero bool false Count zero as well.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A number above zero — or zero itself with allowZero true true
Zero (by default), or a number below it false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

Strict by default: "quantity must be positive". With allowZero: true it is "must not be negative": a balance, a count that may be empty.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsPositive('0.5');   // -> true
CkNumber::IsPositive(0);       // -> false
CkNumber::IsPositive(
    input: 0,
    allowZero: true
);   // -> true

IsNegative

Below zero?

static function IsNegative(
    mixed $input = null,
    bool $allowZero = false,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
allowZero bool false Count zero as well.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A number below zero — or zero itself with allowZero true true
Zero (by default), or a number above it false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

The mirror of IsPositive. IsNegative, IsZero and IsPositive partition the numbers: exactly one of them is true for any value that reads.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsNegative('-0.5');   // -> true
CkNumber::IsNegative(0);        // -> false

IsZero

Exactly zero?

static function IsZero(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A number equal to zero: 0, '0.0', '-0' true true
Any other number false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

The OrNull readers call zero "missing" by default; this says so out loud, for data-quality checks and for telling "nobody set it" from "not a number" — the same job IsNil on CkUuid and IsPlaceholder on CkDate do. Note that IntOrNull('0.4') is null because 0.4 rounds to a missing 0, while IsZero('0.4') is false: it is not zero.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsZero('0.0');    // -> true
CkNumber::IsZero('junk');   // -> false  (not a number, not zero)

IsBetween

Inside a range?

static function IsBetween(
    mixed $input = null,
    mixed $min = null,
    mixed $max = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
min mixed null The lower bound, inclusive, read the same way. null means no lower bound.
max mixed null The upper bound, inclusive, read the same way. null means no upper bound.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says which value is not a number and why — or that the bounds contradict each other — instead of answering false.

Returns

When Default With throwIfInvalid: true
The value and the given bounds read, and the value lies inside, both ends included true true
Everything reads, and the value lies outside false false
Rejected: an unreadable value or bound, or bounds that contradict each other false Throws CkNumberException — carries Input, TargetType and Reason

IsBetween($age, 0, 120). Bounds are read the same way as the value, so '100' and 100 both work, and a null bound is open: IsBetween($qty, 1, null) is "at least one". Comparison is exact, on every digit — a float is read by its first fifteen significant digits, so 0.1 is the one tenth it was meant to be.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsBetween(
    input: '5',
    min: 1,
    max: 10
);   // -> true

CkNumber::IsBetween(
    input: 10,
    min: 1,
    max: 10
);   // -> true   (both ends count)

CkNumber::IsBetween(
    input: '0.15',
    min: 0.1,
    max: 0.2
);   // -> true   (exact decimals)

CkNumber::IsBetween(
    input: 5,
    min: 10,
    max: 1
);   // -> false  (contradictory bounds)

IsAtLeast

At least this much?

static function IsAtLeast(
    mixed $input = null,
    mixed $min = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
min mixed null The lower bound, inclusive, read the same way.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when either value is not a number.

Returns

When Default With throwIfInvalid: true
Both read, and the value is greater than or equal to min true true
Both read, and the value is below min false false
Rejected: either is not a number false Throws CkNumberException — carries Input, TargetType and Reason

IsAtLeast($qty, 1), IsAtLeast($age, 18). The bound counts. The same as IsBetween with an open upper bound, spelled the way validation code reads.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsAtLeast(
    input: 5,
    min: 5
);   // -> true

CkNumber::IsAtLeast(
    input: '4.5',
    min: 5
);   // -> false

IsAtMost

At most this much?

static function IsAtMost(
    mixed $input = null,
    mixed $max = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
max mixed null The upper bound, inclusive, read the same way.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when either value is not a number.

Returns

When Default With throwIfInvalid: true
Both read, and the value is less than or equal to max true true
Both read, and the value is above max false false
Rejected: either is not a number false Throws CkNumberException — carries Input, TargetType and Reason

IsAtMost($items, 100), IsAtMost($rate, 1). The bound counts. The mirror of IsAtLeast.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsAtMost(
    input: 100,
    max: 100
);   // -> true

CkNumber::IsAtMost(
    input: '100.01',
    max: 100
);   // -> false

HasDecimals

No more than so many decimal places?

static function HasDecimals(
    mixed $input = null,
    int $atMost = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
atMost int 0 The most decimal places allowed. Below 0 acts as 0.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A number with at most atMost decimal places once trailing zeros are dropped true true
A number that needs more false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

Judged on the value, not its spelling: '12.50' has one decimal place, '12.00' has none, 12.345 has three. For quantities and rates with a precision limit — "no more than two decimals". With the default of 0 it is IsInteger.

use SafeCoder\CommonKit\CkNumber;

CkNumber::HasDecimals(
    input: '12.50',
    atMost: 1
);   // -> true

CkNumber::HasDecimals(
    input: '12.50',
    atMost: 0
);   // -> false

CkNumber::HasDecimals(
    input: '12.345',
    atMost: 2
);   // -> false

IsEven

An even whole number?

static function IsEven(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A whole, even number: 2, '4', '10.0', 0 true true
An odd number, or one with a fraction, which has no parity false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

IsOdd is the twin. A whole number is one or the other; a fraction is neither, and so is anything that is not a number — so neither method is the other's negation.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsEven('10.0');   // -> true
CkNumber::IsEven('2.5');    // -> false  (no parity)

IsOdd

An odd whole number?

static function IsOdd(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering false, when the value is not a number.

Returns

When Default With throwIfInvalid: true
A whole, odd number: 3, '-3', '7.0' true true
An even number, or one with a fraction false false
Rejected: not a number — missing, grouping or a currency symbol, a boolean, an array, NAN false Throws CkNumberException — carries Input, TargetType and Reason

The twin of IsEven, with the same rules.

use SafeCoder\CommonKit\CkNumber;

CkNumber::IsOdd('-3');   // -> true
CkNumber::IsOdd(0);      // -> false

Clamp

Read it, and keep it within whole-number bounds.

static function Clamp(
    mixed $input = null,
    int $min = 0,
    int $max = 0,
    ?int $fallback = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
min int 0 The smallest value allowed.
max int 0 The largest value allowed.
fallback ?int null What comes back when the value cannot be read, or the bounds contradict each other; the lower bound when not given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be read or the bounds contradict each other.

Returns

When Default With throwIfInvalid: true
The value reads and lies inside the bounds The value, an int — a fraction rounding as the whole-number readers round The value
It reads and lies outside The nearer bound The nearer bound
Rejected: not a number, or bounds that contradict each other The fallback, the lower bound by default Throws CkNumberException — carries Input, TargetType and Reason

Page sizes, retry counts, a LIMIT — anything a caller may set but not set wildly. The answer is always an int; for a range with a fraction in it, ClampFloat. The value is read as widely as it can be before the bounds apply, so '300' still clamps to a TINYINT's 255 and '1e30' to the top.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Clamp(
    input: '250',
    min: 1,
    max: 100
);   // -> 100

CkNumber::Clamp(
    input: '12.5',
    min: 1,
    max: 100
);   // -> 12  (inside: the value, rounded)

CkNumber::Clamp(
    input: 'junk',
    min: 1,
    max: 100
);   // -> 1  (unreadable: the lower bound)

CkNumber::Clamp(
    input: 'junk',
    min: 1,
    max: 100,
    fallback: 20
);   // -> 20  (unreadable: the fallback you named)

ClampFloat

Read it, and keep it within bounds with a fraction in them.

static function ClampFloat(
    mixed $input = null,
    float $min = 0.0,
    float $max = 0.0,
    ?float $fallback = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): float
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.
min float 0.0 The smallest value allowed.
max float 0.0 The largest value allowed.
fallback ?float null What comes back when the value cannot be read, the bounds contradict each other or a bound is NAN or an infinity; the lower bound when not given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback.

Returns

When Default With throwIfInvalid: true
The value reads and lies inside the bounds The value, a float, its fraction kept The value
It reads and lies outside The nearer bound The nearer bound
Rejected: not a number, bounds that contradict each other, or a bound that is NAN or an infinity The fallback, the lower bound by default — 0.0 when the lower bound is the one refused Throws CkNumberException — carries Input, TargetType and Reason

A ratio, a percentage, a weight — anything with a fraction a caller may set but not set wildly. The answer is always a float; for whole numbers, Clamp. A bound that is NAN or an infinity bounds nothing, so it is refused and the fallback answers.

use SafeCoder\CommonKit\CkNumber;

CkNumber::ClampFloat(
    input: '0.75',
    min: 0.0,
    max: 1.0
);   // -> 0.75

CkNumber::ClampFloat(
    input: '7',
    min: 0.0,
    max: 1.0
);   // -> 1.0  (above: the top)

CkNumber::ClampFloat(
    input: 'junk',
    min: 0.0,
    max: 1.0
);   // -> 0.0  (unreadable: the lower bound)

Round

Read it, and round it the stated way.

static function Round(
    mixed $input = null,
    int $decimals = 0,
    MidpointRounding $rounding = CkSettings::NumberRounding,
    float $fallback = 0.0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): float
Parameter Type Default What it does
input mixed null The value: a number, or text.
decimals int 0 The places to keep, 0 to CkSettings::MaxDecimalPlaces (28); outside acts as the nearer end.
rounding MidpointRounding CkSettings::NumberRounding (MidpointRounding::ToEven) How a half rounds. AwayFromZero is the schoolbook rule.
fallback float 0.0 What comes back when the value cannot be read.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the value cannot be read.

Returns

When Default With throwIfInvalid: true
The value reads The rounded value — exact in decimal, handed back as the nearest float The rounded value
Rejected: not a number The fallback Throws CkNumberException — carries Input, TargetType and Reason

Halves go to the even neighbour by default — Round(2.5) is 2, Round(3.5) is 4 — the same rule the integer readers apply, and the one that surprises everyone once. Pass MidpointRounding::AwayFromZero for 2.5 to become 3; CkMoney::Round uses that rule for money. The value is read as the readers read it — text digit by digit, a float by its first fifteen significant digits — and rounded exactly from there.

use SafeCoder\CommonKit\CkNumber;
use SafeCoder\CommonKit\Enums\MidpointRounding;

CkNumber::Round(
    input: '2.345',
    decimals: 2
);   // -> 2.34

CkNumber::Round(2.5);   // -> 2.0   (halves to even)

CkNumber::Round(
    input: 2.5,
    rounding: MidpointRounding::AwayFromZero
);   // -> 3.0

Divide

A quotient that cannot crash.

static function Divide(
    mixed $numerator = null,
    mixed $denominator = null,
    float $fallback = 0.0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): float
Parameter Type Default What it does
numerator mixed null The value to divide: a number, or text.
denominator mixed null The value to divide by, read the same way.
fallback float 0.0 What comes back when a value cannot be read, the denominator is zero, or the quotient is more than a float can hold.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkNumberException that says why, instead of answering the fallback, when the division cannot be done.

Returns

When Default With throwIfInvalid: true
Both read and the denominator is not zero The quotient — exact in decimal, handed back as the nearest float The quotient
Rejected: a value that is not a number, a zero denominator, a quotient more than a float can hold The fallback Throws CkNumberException — carries Input, TargetType and Reason — "division by zero" names the denominator

Divide($sold, $stocked) for a ratio, Divide($total, $count) for an average. Division by zero is the commonest crash in reporting code; here it is the fallback, or a reason. The arithmetic is exact, digit for digit, and only the answer is a float — so Divide(0.1 + 0.2, 0.3) is exactly 1.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Divide(
    numerator: 1,
    denominator: 4
);   // -> 0.25

CkNumber::Divide(
    numerator: '10',
    denominator: '3'
);   // -> 3.3333333333333335  (the float nearest to three and a third)

CkNumber::Divide(
    numerator: 5,
    denominator: 0,
    fallback: -1.0
);   // -> -1.0

CkNumber::Divide(
    numerator: 0.1 + 0.2,
    denominator: 0.3
);   // -> 1.0  (exact: the floats are read by their fifteen digits)

Explain

Why not — in words, without an exception.

static function Explain(
    mixed $input = null
): string
Parameter Type Default What it does
input mixed null The value: a number, or plain invariant text.

Returns

When You get
The value passes the reader ''
It does not The reason, the same words the exception would carry — an answer, not a failure; it never throws

The form-friendly twin of throwIfInvalid: the message to show beside the field, without a try and catch. Every class with a reader has one, on the reader's own rules, so the reason is exactly what the reader would say.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Explain('42');       // -> ''
CkNumber::Explain('1,234');    // -> 'not a number'
CkNumber::Explain(null);       // -> 'no value'
CkNumber::Explain(true);       // -> 'a Boolean is not a number'

Good to know

  • There is no ceiling on the digits. Decimal keeps every digit, the comparisons compare on every digit, and the whole-number readers read text digit by digit, never through a float. Only the methods that answer a float — Float, Round, Divide, Percent — are held to what a float holds: the arithmetic is exact and the answer is the nearest float to it.
  • Grouping and currency symbols are rejected. '1,234' and '$5' answer the fallback. Text dressed as money — symbols, thousands separators, accounting negatives — belongs to CkMoney.
  • The column readers are unsigned by default. TinyInt, SmallInt, MediumInt, Integer and BigInt refuse a negative value until told unsigned: false — and a PostgreSQL column, which is never unsigned, is always told.
  • There is no plain Int, on purpose. Every whole number is read by the column that will hold it, so the choice is always made: Integer for an INT column, four bytes, BigInt for a BIGINT, eight. A PHP int with no column in mind is BigInt with unsigned: false. Reading an INT column's value as eight bytes would let 3,000,000,000 through, which the column cannot hold.
  • Halves round to the even neighbour. 12.5 becomes 12 and 13.5 becomes 14 — banker's rounding, so long runs of .5 values do not all drift upward. Pass exact: true when a fraction should be refused instead.
  • IntOrNull('0.4') is null. The fraction rounds to 0 first, and the OrNull family reads zero as missing by default. Pass treatZeroAsMissing: false when small values must survive as 0.
  • A bare percent is a rate as it stands. Percent(15) is 15.0 — that is 1,500%, not 15%. Only a trailing % divides by 100. Send the sign or divide upstream.
  • NAN and the infinities are not numbers here. Neither are booleans or arrays — they all answer the fallback (or null from the twins). A boolean is never read as 1 or 0: that is CkBool's rule, and it runs the other way.
  • Reading is locale-independent. The decimal point is always a dot, whatever the server's regional settings — the same text reads the same everywhere.
  • A 0 result can mean two things — a real zero, or a rejected value falling back to the default. When you need to know which, use the OrNull twin, pick a fallback you can recognise, or ask for an exception.
  • The validators treat zero as a number. IsNumber('0'), IsInteger(0) and IsBetween(0, 0, 10) are all true, whatever the OrNull family says about zero; IsZero is the one that asks about it. All of them answer false for anything unreadable, and every one takes throwIfInvalid.
  • Comparisons are exact. IsBetween, IsAtLeast and IsAtMost compare on every digit, a float read by its first fifteen significant digits. HasDecimals counts on the value, so '12.50' has one decimal place.

If you want errors instead

Pass throwIfInvalid: true and a value that cannot be converted raises a CkNumberException (from SafeCoder\CommonKit\Exceptions) instead of answering the fallback. The exception carries the Input, the TargetType and a short Reason, flattened into a one-line message.

use SafeCoder\CommonKit\CkNumber;

CkNumber::Integer(
    input: 'twelve',
    throwIfInvalid: true
);   // throws CkNumberException — the message says why

The OrNull twins have no such switch — they never throw: null is their answer for anything unusable. When you want an error instead, use the plain sibling with throwIfInvalid: true.