Package: Kernel-Numbers

Class: Number

Introduction

Environment: container

A common abstract superclass for all Number implementations. It is the superclass of all classes that represent numbers, for example Integer, Float, and Fraction, etc. The binary operators +,=,*,/ are defined for all numbers, so we can do arithmetic.

For example:

1+2*8

We support traditional arithmetic precedence, so the above expression is equivalent to:

1+(2*8)

Class Method

Category: instance creation

  • new - Create a new number object. The default value is 1.

Instance Method

Category: arithmetic

  • * - Multiply two numbers.

  • + - Add two numbers.

  • - - Subtract two numbers.

  • / - Divide two numbers.

  • abs - answer a number that is the absolute value (positive magnitude) of the receiver.

    For example:

    "ab = 243.2"
    ab := -243.2 abs
  • even - Answer whether the receiver is an even number.

    For example:

    4 even = true.
  • floor - answer a Number that is the floor value (positive magnitude) of the receiver.

    For example:

    "ab = 25"
    ab := 25.75 floor
  • odd - Answer whether the receiver is an odd number.

    For example:

    3 odd = true.
  • raisedTo: - answer the receiver raised to aNumber.

    For example:

    (2 raisedTo: 8) = 256.
    (8 raisedTo: 2) = 64.
    (2 raisedTo: (1/12)) = 1.0.
    (2 raisedTo: -1) = (1/2).

Category: comparing

  • < - Compare the receiver with the argument and answer with true if the receiver is less than the argument. Otherwise answer false.

  • <= - Compare the receiver with the argument and answer true if the receiver is less than or equal to the argument. Otherwise answer false.

  • = - Compare the receiver with the argument and answer true if the receiver is equal to the argument. Otherwise answer false.

  • > - answer whether the receiver sorts after aString. The collation order is simple ascii (with case differences).

  • >= - Compare the receiver with the argument and answer true if the receiver is greater than or equal to the argument. Otherwise answer false.

Category: controlling

  • timesRepeat: - Evaluate the argument, aBlock, the number of times represented by the receiver.

    For example:

    "Print aaa"
    3 timesRepeat: [ Console print: 'a'].
  • to:by:do: - Normally compiled in-line, and therefore not overridable. Evaluate aBlock for each element of the interval (self to: stop by: step).

    For example:

    "Print 0 2 4 6"
    0 to: 6 by: 2 do: [:i | Console print: i, ' '].
  • to:do: - Normally compiled in-line, and therefore not overridable. Evaluate aBlock for each element of the interval (self to: stop by: 1).

    For example:

    "Print 1 2 3 4 5"
    1 to: 5 do: [:i | Console print: i, ' '].

Category: converting

  • asCustomNumber: - The custom numeric format specifiers

    The custom numeric format specifiers answer a string that is the receiver formatted according to the format string. The format string is a string that contains a number of placeholders that are replaced by the corresponding values of the receiver.

    Here is a list of available format String.

    [0] ** Zero placeholder**. Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

    Example:

    (12.34 asCustomNumber: '0') = '12'.
    (12.34 asCustomNumber: '00') = '12'.
    (12.34 asCustomNumber: '000') = '012'.
    (12.34 asCustomNumber: '0000') = '0012'.
    (12.44 asCustomNumber: '00') = '0013'.

    [#] Digit placeholder. Replaces the “#” symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

    No digit appears in the result string if the corresponding digit in the input string is a non-significant 0.

    Example:

    (12.34 asCustomNumber: '#') = '12'.
    (12.34 asCustomNumber: '##') = '12'.
    (12.34 asCustomNumber: '###') = '12'.
    (12.34 asCustomNumber: '####') = '12'.
    (12.44 asCustomNumber: '##') = '0013'.

    .

    [.] Decimal point. Determines the location of the decimal separator in the result string.

    Example:

    (12.34 asCustomNumber: '0.0') = '12.3'.
    (12.44 asCustomNumber: '.00') = '12.34.
    (12.44 asCustomNumber: '.') = '12'.

    [,] Group separator and number scaling. Can be used as both a group separator (also known as a thousand separator) and a number scaling specifier.

    • As a group separator, it inserts a localized group separator character between each group.

    • As a number scaling specifier, it divides a number by 1000 for each comma specified.

    To specify a group separator, place one or more commas between two digit placeholders (0 or #) that format the integral digits of a number. This results in a group separator character being inserted between each number group in the integral part of the output.

    To specify a number scaling specifier, place one or more commas immediately to the left of the explicit or implicit decimal point.

    Example - As a Group Separator:

    (12345678 asCustomNumber: '#,#') = '12,345,678'.

    Example – As a Number Scaling Specifier:

    (12000 asCustomNumber: '#,') = '12'.
    (1234567890 asCustomNumber: '#,,') = '1235'.

    Example – As Both:

    (1234567890 asCustomNumber: '#,#,') = '1,234,568'.
    (1234567890 asCustomNumber: '#,#,,') = '1,235'.

    [%] Percentage placeholder. Multiplies a number by 100 and inserts a localized percentage symbol in the result string.

    Example:

    (0.1234 asCustomNumber: '#.# %') = '12.3 %'.
    (0.1235 asCustomNumber: '#.#%') = '12.4%'.
    (0.125 asCustomNumber: '#. %') = '13 %'.
    (0.1234 asCustomNumber: '#.#%') = '12.3%'.

    [] Per mille placeholder. Multiplies a number by 1000 and inserts a localized per mille symbol in the result string.

    Example:

    (0.01234 asCustomNumber: '#.# ‰') = '12.3 ‰'.
    (0.01235 asCustomNumber: '#.# ‰') = '12.4 ‰'.
    (0.0125 asCustomNumber: '#. ‰') = '13 ‰'.
    (0.01234 asCustomNumber: '#.# ‰') = '12.3 ‰'.

    [E0 E+0 E-0 e0 e+0 e-0] Exponential notation. If followed by at least one zero (0), formats the result using exponential notation. The case (E or e) indicates the case of the exponent symbol in the result string. The number of zeros following the E or e character determines the minimum number of digits in the exponent. A plus sign (+) indicates that a sign character always precedes the exponent. A minus sign (-) indicates that a sign character precedes only negative exponents.

    Example:

    (123456789 asCustomNumber: '0e0') = '1e8'.
    (123456789 asCustomNumber: '0e+0') = '1e+8'.
    (123456789 asCustomNumber: '0E+00') = '1E+08'.
    (1234.56789 asCustomNumber: '0.0##e+00') = '1.235e+03'.
    (12.3456789-12 asCustomNumber: '0e-0') = '3e-1'.

    [\] Escape character. Causes the next character to be interpreted as a literal rather than as a custom format specifier.

    Example:

    (123 asCustomNumber: '\#0000') = '#0123'.

    ['string' "string"] Literal string delimiter. Indicates that the enclosed characters should be copied to the result string unchanged.

    Example:

    (23 asCustomNumber: '## Degrees') = '23 Degrees'.

    [;, Other] All other characters. The character is copied to the result string unchanged.

    Example:

    (12 asCustomNumber: '# °C') = '12 °C'.
  • asInteger - return the integer present in the receiver, or nil. In case of float, returns the integer part.

    For example:

    120 asInteger = 120.
    120.3 asInteger = 120.
  • asString - Converts a number into a string.

    For example:

    str := 12 asString. "returns '12'"
    str := 12.3 asString. "returns '12.3'"

Category: covnerting

  • asCharacter - Answer the Character whose value is the receiver.

    For example:

    65 asCharacter = $A.

Last updated