simple_decimal

API Reference

Contents

Creation Procedures

default_create

create d.default_create

Creates a zero decimal value.

Postcondition: is_zero

make

create d.make (a_value: READABLE_STRING_GENERAL)

Creates from string representation. Accepts various formats including currency symbols and thousand separators.

Accepted formats: "19.99", "$19.99", "$1,234.56", "-$99.99", "1,000,000"

Precondition: a_value /= Void and then not a_value.is_empty

Example:

create price.make ("$1,234.56")   -- Creates 1234.56

make_from_integer

create d.make_from_integer (a_value: INTEGER)

Creates from an integer value.

Postcondition: to_integer = a_value

Example:

create count.make_from_integer (42)

make_from_double

create d.make_from_double (a_value: DOUBLE)

Creates from a double value. Note: Prefer make with string for exact precision.

The double 0.1 is already imprecise before conversion. Use string creation for financial applications.

make_from_decimal

create d.make_from_decimal (a_decimal: MA_DECIMAL)

Creates from an existing Gobo MA_DECIMAL. Useful for advanced interoperability.

Precondition: a_decimal /= Void

make_currency

create d.make_currency (a_dollars: INTEGER; a_cents: INTEGER)

Creates from separate dollar and cent amounts.

Precondition: a_cents >= 0 and a_cents <= 99

Example:

create price.make_currency (19, 99)   -- Creates 19.99
create price.make_currency (-5, 25)   -- Creates -5.25

make_zero

create d.make_zero

Creates a zero value.

Postcondition: is_zero

make_one

create d.make_one

Creates the value 1.

Postcondition: to_integer = 1

Access (Attributes)

decimal

decimal: MA_DECIMAL

The underlying Gobo decimal value. Exposed for advanced users who need direct access to MA_DECIMAL features.

context

context: MA_DECIMAL_CONTEXT

The decimal context used for operations. Controls precision and rounding behavior.

Status Queries

is_zero

is_zero: BOOLEAN

Returns True if the value equals zero.

is_negative

is_negative: BOOLEAN

Returns True if the value is less than zero.

is_positive

is_positive: BOOLEAN

Returns True if the value is greater than zero (not zero, not negative).

is_integer

is_integer: BOOLEAN

Returns True if the value has no fractional part (e.g., 42.00 returns True, 42.5 returns False).

is_nan

is_nan: BOOLEAN

Returns True if the value is Not-a-Number (result of invalid operation).

is_infinity

is_infinity: BOOLEAN

Returns True if the value is positive or negative infinity.

Conversion

to_integer

to_integer: INTEGER

Converts to integer, truncating any fractional part.

Preconditions: not is_nan, not is_infinity

to_double

to_double: DOUBLE

Converts to double. May lose precision for values that cannot be exactly represented.

Preconditions: not is_nan, not is_infinity

out

out: STRING

Standard string representation (may use scientific notation for very large/small values).

Example: "19.99", "1.5E+10"

to_string

to_string: STRING

Engineering string representation (avoids scientific notation when possible).

Example: "19.99", "1234567890"

to_currency_string

to_currency_string: STRING

Formats as currency with dollar sign, comma separators, and exactly 2 decimal places.

Examples: "$1,234.56", "-$99.99", "$0.50"

Postcondition: Result starts with "$" or "-$"

dollars

dollars: INTEGER

Returns the integer dollar portion (absolute value).

Example: For -19.95, returns 19

cents

cents: INTEGER

Returns the cents portion (0-99).

Postcondition: Result >= 0 and Result <= 99

Example: For 19.95, returns 95

Comparison

is_less alias "<"

is_less alias "<" (other: like Current): BOOLEAN

Returns True if current value is less than other.

is_equal

is_equal (other: like Current): BOOLEAN

Returns True if values are mathematically equal.

Inherited from COMPARABLE:

Arithmetic Operations

All arithmetic operations are immutable - they return new SIMPLE_DECIMAL objects without modifying the originals.

add alias "+"

add alias "+" (other: like Current): SIMPLE_DECIMAL

Returns the sum of current and other.

Precondition: other /= Void

Postcondition: Result /= Void

subtract alias "-"

subtract alias "-" (other: like Current): SIMPLE_DECIMAL

Returns the difference (current minus other).

Precondition: other /= Void

Postcondition: Result /= Void

multiply alias "*"

multiply alias "*" (other: like Current): SIMPLE_DECIMAL

Returns the product of current and other.

Precondition: other /= Void

Postcondition: Result /= Void

divide alias "/"

divide alias "/" (other: like Current): SIMPLE_DECIMAL

Returns the quotient (current divided by other).

Preconditions: other /= Void, not other.is_zero

Postcondition: Result /= Void

integer_divide alias "//"

integer_divide alias "//" (other: like Current): SIMPLE_DECIMAL

Returns the integer quotient (floor division).

Preconditions: other /= Void, not other.is_zero

Postconditions: Result /= Void, Result.is_integer

Example: 100 // 30 = 3

modulo alias "\\"

modulo alias "\\" (other: like Current): SIMPLE_DECIMAL

Returns the remainder after integer division.

Preconditions: other /= Void, not other.is_zero

Postcondition: Result /= Void

Example: 100 \\ 30 = 10

negate

negate: SIMPLE_DECIMAL

Returns the negated value (sign flipped).

Postconditions: Result /= Void, Result.is_negative /= is_negative or is_zero

absolute

absolute: SIMPLE_DECIMAL

Returns the absolute value.

Postconditions: Result /= Void, not Result.is_negative

power

power (n: INTEGER): SIMPLE_DECIMAL

Returns current raised to the integer power n.

Postcondition: Result /= Void

Example: 10.power(3) = 1000

Rounding

round_cents

round_cents: SIMPLE_DECIMAL

Rounds to 2 decimal places using banker's rounding. Shortcut for round(2).

Postcondition: Result /= Void

round

round (a_places: INTEGER): SIMPLE_DECIMAL

Rounds to specified decimal places using banker's rounding (half-even).

Precondition: a_places >= 0

Postcondition: Result /= Void

round_up

round_up (a_places: INTEGER): SIMPLE_DECIMAL

Rounds away from zero to specified decimal places.

Precondition: a_places >= 0

Examples: 2.34 -> 2.4, -2.34 -> -2.4

round_down

round_down (a_places: INTEGER): SIMPLE_DECIMAL

Rounds toward zero to specified decimal places (truncation).

Precondition: a_places >= 0

Examples: 2.36 -> 2.3, -2.36 -> -2.3

round_ceiling

round_ceiling (a_places: INTEGER): SIMPLE_DECIMAL

Rounds toward positive infinity to specified decimal places.

Precondition: a_places >= 0

Examples: 2.34 -> 2.4, -2.36 -> -2.3

round_floor

round_floor (a_places: INTEGER): SIMPLE_DECIMAL

Rounds toward negative infinity to specified decimal places.

Precondition: a_places >= 0

Examples: 2.36 -> 2.3, -2.34 -> -2.4

truncate

truncate: SIMPLE_DECIMAL

Removes the fractional part entirely. Equivalent to round_down(0).

Postconditions: Result /= Void, Result.is_integer

Financial Operations

percent_of

percent_of (a_base: like Current): SIMPLE_DECIMAL

Calculates what percentage current is of the base value.

Preconditions: a_base /= Void, not a_base.is_zero

Example:

create tip.make ("15.00")
create bill.make ("75.00")
tip.percent_of (bill)  -- Returns 20.0 (tip is 20% of bill)

as_percentage

as_percentage: SIMPLE_DECIMAL

Multiplies by 100 (converts decimal rate to percentage display).

Example: 0.0825.as_percentage = 8.25

from_percentage

from_percentage: SIMPLE_DECIMAL

Divides by 100 (converts percentage to decimal rate for multiplication).

Example: 8.25.from_percentage = 0.0825

add_percent

add_percent (a_percent: like Current): SIMPLE_DECIMAL

Adds a percentage to current value. The percentage is given as a number (e.g., 8.25 for 8.25%).

Precondition: a_percent /= Void

Formula: current + (current * a_percent / 100)

Example:

create price.make ("100.00")
create tax_rate.make ("8.25")
price.add_percent (tax_rate)  -- Returns 108.25

subtract_percent

subtract_percent (a_percent: like Current): SIMPLE_DECIMAL

Subtracts a percentage from current value (discount).

Precondition: a_percent /= Void

Formula: current - (current * a_percent / 100)

Example:

create price.make ("100.00")
create discount.make ("15.0")
price.subtract_percent (discount)  -- Returns 85.00

split

split (n: INTEGER): ARRAYED_LIST [SIMPLE_DECIMAL]

Splits the amount into n equal parts, distributing any remainder cents to the first parts.

Precondition: n > 0

Postconditions: Result.count = n, sum of Result equals original (rounded to cents)

Example:

create bill.make ("100.00")
parts := bill.split (3)
-- Result: [$33.34, $33.33, $33.33]
-- Sum: $100.00 exactly

Factory Helpers

zero

zero: SIMPLE_DECIMAL

Returns a new zero value.

Postcondition: Result.is_zero

one

one: SIMPLE_DECIMAL

Returns a new value of 1.

Postcondition: Result.to_integer = 1

Class Invariant

invariant
    decimal_exists: decimal /= Void
    context_exists: context /= Void