Decimal.from_text
Decimal.from_texts mc
Group: Conversions
Documentation
Construct a Decimal from a Text.
Arguments
s: TheTextto construct aDecimalfrom.mc: TheMath_Contextto use to specify precision andRounding_Mode. If aMath_Contextis used, there is a possibility of a loss of precision.
Errors
- If a
Textargument is incorrectly formatted, aNumber_Parse_Erroris thrown. - If the construction of the Decimal results in a loss of precision, a
Loss_Of_Numeric_Precisionwarning is attached. This can happen in the followoing ways:- A
Floatvalue is implicitly converted to aDecimal(either by callingDecimal.fromor by passing theFloatas a parameter of typeDecimal) - A
Math_Contextvalue is explicitly passed, and the precision it specifies is not sufficient to precisely represent the argument to be converted
- A
- If an arithmetic operation is used on mixed arguments (a
Decimaland aFloat), anIllegal_Argumenterror is thrown. - If an arithmetic operation is used on a
Decimalthat was implicitly converted from aFloat(either by callingDecimal.fromor by passing theFloatas a parameter of typeDecimal), anIllegal_Argumenterror is thrown. - If a floating-poing argument is
NaNor+/- Inf, anIllegal_Argumenterror is thrown.
^ Example
Create a Decimal from a Text.
d = Decimal.from_text "12.345"
^ Example
Create a Decimal from a Text.
d = 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.