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:
We support traditional arithmetic precedence, so the above expression is equivalent to:
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:
even
- Answer whether the receiver is an even number.For example:
floor
- answer a Number that is the floor value (positive magnitude) of the receiver.For example:
odd
- Answer whether the receiver is an odd number.For example:
raisedTo:
- answer the receiver raised to aNumber.For example:
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.isFloat
- Test if object is float.For example:
isInteger
- Test if object is integer.For example:
isZero
- Test if number is zero.For example:
max:
- Answer the greater of the two numbers in argumentsFor example:
min:
- Answer the lesser of the two numbers in argumentsFor example:
negative
- Test if number is negative.For example:
positive
- Test if number is positive.For example:
strictlyPositive
- test if number is greater than zero.For example:
Category: controlling
timesRepeat:
- Evaluate the argument, aBlock, the number of times represented by the receiver.For example:
to:
- Normally compiled in-line, and therefore not overridable.Build an array of the integers from the receiver to the argument, aNumber, and evaluate aBlock with each of the integers as the argument.
For example:
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:
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:
Category: converting
asCharacter
- Answer the Character whose value is the receiver.For example:
asCustomNumber:
- The custom numeric format specifiersThe 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:
[
#
] 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:
.
[
.
] Decimal point. Determines the location of the decimal separator in the result string.Example:
[
,
] 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:
Example – As a Number Scaling Specifier:
Example – As Both:
[
%
] Percentage placeholder. Multiplies a number by 100 and inserts a localized percentage symbol in the result string.Example:
[
‰
] Per mille placeholder. Multiplies a number by 1000 and inserts a localized per mille symbol in the result string.Example:
[
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
ore
) indicates the case of the exponent symbol in the result string. The number of zeros following theE
ore
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:
[
\
] Escape character. Causes the next character to be interpreted as a literal rather than as a custom format specifier.Example:
[
'string'
"string"
] Literal string delimiter. Indicates that the enclosed characters should be copied to the result string unchanged.Example:
[
;
, Other] All other characters. The character is copied to the result string unchanged.Example:
asFloat
- Answer the number as a float data type.For example:
asInteger
- return the integer present in the receiver, or nil. In case of float, returns the integer part.For example:
asString
- Converts a number into a string.For example:
roundTo:
- Answer the number, rounded to the nearest multiple of the other argument.For example:
truncateTo:
- Answer the number, truncated to the nearest multiple of the other argument.For example:
Category: math
cos
- Answer the cosine of the number. The argument is assumed to be in radians.For example:
exp
- Answer the e raised to the number.For example:
ln
- Answer the natural log (base e) of the number.For example:
log:
- Answer the log of the number, base of number in second argument.For example:
sin
- Answer the sine of the number. The angle is assumed to be in radians.For example:
sqrt
- Answer the square root of the number.For example:
squared
- Answer the square of the number.For example:
tan
- Answer the tangent of the number. The angle is assumed to be in radians.For example:
tanh
- Answer the hyperbolic tangent of the number. The angle is assumed to be in radians.For example:
Last updated