Skip to main content

Decimal.new

Decimal.newx mc

Documentation

Construct a Decimal from a Text, Integer or Float.

Arguments

  • x: The Text, Integer, or Float to construct a Decimal from.
  • mc: The Math_Context to use to specify precision and Rounding_Mode. If a Math_Context is used, there is a possibility of a loss of precision.

Errors

  • If a Text argument is incorrectly formatted, a Number_Parse_Error is thrown.
  • If the construction of the Decimal results in a loss of precision, a Loss_Of_Numeric_Precision warning is attached. This can happen in the followoing ways:
    • A Float value is implicitly converted to a Decimal (either by calling Decimal.from or by passing the Float as a parameter of type Decimal)
    • A Math_Context value is explicitly passed, and the precision it specifies is not sufficient to precisely represent the argument to be converted
  • If an arithmetic operation is used on mixed arguments (a Decimal and a Float), an Illegal_Argument error is thrown.
  • If an arithmetic operation is used on a Decimal that was implicitly converted from a Float (either by calling Decimal.from or by passing the Float as a parameter of type Decimal), an Illegal_Argument error is thrown.
  • If a floating-poing argument is NaN or +/- Inf, an Illegal_Argument error is thrown.

^ Example Create a Decimal from a Text.

c = Decimal.from_text "12.345"

^ Example Create a Decimal from an Integer.

c = 12345.to_decimal

^ Example Create a Decimal from a Float.

c = 12.345.to_decimal

^ Example Create a Decimal from a value.

c = dec 12.345

Remarks

Number Format

The textual format for a Decimal is defined at https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#BigDecimal-java.lang.String-.

Creating Decimals and Converting to Decimal

When creating a Decimal from a literal floating-point value, the preferred method is to express the literal as a string and use Decimal.from_text, since this will give a Decimal that matches the value precisely.

To convert a Float or Integer to a Decimal, use its .to_decimal method.

You can also use the convenience method dec to convert any Integer, Float, or Text value to a Decimal. dec does not attach a warning.