The Numeric routine

Combined from primary sources listed below.

In class IO::Path ( Type/IO/Path )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(IO::Path:D: --> Numeric:D)

Coerces .basename to Numeric. Fails with X::Str::Numeric if base name is not numerical.

In class Nil ( Type/Nil )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric()

Warns the user that they tried to numify a Nil.

In role Sequence ( Type/Sequence )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(::?CLASS:D:)

Returns the number of elements in the cached sequence.

In class NumStr ( Type/NumStr )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(NumStr:D: --> Num:D)
multi method Numeric(NumStr:U: --> Num:D)

The :D variant returns the numeric portion of the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns value 0e0.

In class Map ( Type/Map )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(Map:D: --> Int:D)

Returns the number of pairs stored in the Map (same as .elems).

my $m = Map.new('a' => 2, 'b' => 17);
say $m.Numeric;                                   # OUTPUT: «2␤»

In class ComplexStr ( Type/ComplexStr )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(ComplexStr:D: --> Complex:D)
multi method Numeric(ComplexStr:U: --> Complex:D)

The :D variant returns the numeric portion of the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns value <0+0i>.

In class Str ( Type/Str )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(Str:D: --> Numeric:D)

Coerces the string to Numeric using semantics equivalent to val routine. Fails with X::Str::Numeric if the coercion to a number cannot be done.

Only Unicode characters with property Nd, as well as leading and trailing whitespace are allowed, with the special case of the empty string being coerced to 0. Synthetic codepoints (e.g. "7\x[308]") are forbidden.

While Nl and No characters can be used as numeric literals in the language, their conversion via Str.Numeric will fail, by design; the same will happen with synthetic numerics (composed of numbers and diacritic marks). See unival if you need to coerce such characters to Numeric. +, - and the Unicode MINUS SIGN − are all allowed.

" −33".Numeric;       # OUTPUT: «-33␤»

In class X::AdHoc ( Type/X/AdHoc )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric()

Converts the payload to Numeric and returns it

In role Numeric ( Type/Numeric )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(Numeric:D: --> Numeric:D)
multi method Numeric(Numeric:U: --> Numeric:D)

The :D variant simply returns the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns self.new.

In class Capture ( Type/Capture )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(Capture:D: --> Int:D)

Returns the number of positional elements in the Capture.

say \(1,2,3, apples => 2).Numeric# OUTPUT: «3␤»

In class Date ( Type/Date )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(Date:D: --> Int:D)

Converts the invocant to Int. The same value can be obtained with the daycount method. This allows Date objects to be used directly in arithmetic operations.

Available as of release 2023.02 of the Rakudo compiler.

In class StrDistance ( Type/StrDistance )§

See primary docmentation in context for method Numeric.

method Numeric§

Returns the distance as a number.

In enum Endian ( Type/Endian )§

See primary docmentation in context for routine Numeric.

routine Numeric§

multi method Numeric(Endian:D --> Int:D)

Returns the value part of the enum pair.

say NativeEndian.Numeric;    # OUTPUT: «0␤»
say LittleEndian.Numeric;    # OUTPUT: «1␤»
say BigEndian.Numeric;       # OUTPUT: «2␤»

Note that the actual numeric values are subject to change. So please use the named values instead.

In class RatStr ( Type/RatStr )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(RatStr:D: --> Rat:D)
multi method Numeric(RatStr:U: --> Rat:D)

The :D variant returns the numeric portion of the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns value 0.0.

In enum Bool ( Type/Bool )§

See primary docmentation in context for routine Numeric.

routine Numeric§

multi method Numeric(Bool:D --> Int:D)

Returns the value part of the enum pair.

say False.Numeric;                                # OUTPUT: «0␤»
say True.Numeric;                                 # OUTPUT: «1␤»

In role Enumeration ( Type/Enumeration )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(::?CLASS:D:)

Takes a value of an enum and returns it after coercion to Numeric:

enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' );
say cool.Numeric;       # OUTPUT: «42␤»
say almost-pi.Numeric;  # OUTPUT: «3.14␤»
say sqrt-n-one.Numeric# OUTPUT: «0+1i␤»

Note that if the value cannot be coerced to Numeric, an exception will be thrown.

In class List ( Type/List )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(List:D: --> Int:D)

Returns the number of elements in the list (same as .elems).

say (1,2,3,4,5).Numeric# OUTPUT: «5␤»

In class DateTime ( Type/DateTime )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(DateTime:D: --> Instant:D)

Available as of the 2021.09 release of the Rakudo compiler.

Converts the invocant to Instant. The same value can be obtained with the Instant method. This allows DateTime objects to be used directly in arithmetic operations.

In class Thread ( Type/Thread )§

See primary docmentation in context for method Numeric.

method Numeric§

method Numeric(Thread:D: --> Int:D)

Returns a numeric, unique thread identifier, i.e. the same as id.

In class IntStr ( Type/IntStr )§

See primary docmentation in context for method Numeric.

method Numeric§

multi method Numeric(IntStr:D: --> Int:D)
multi method Numeric(IntStr:U: --> Int:D)

The :D variant returns the numeric portion of the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns value 0.