decimal() : Convert to Decimal
On this page
Summary
Convert a value to a decimal number with exact precision. Best for financial calculations.
Signature
decimal(value)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
value |
Any | Yes | The value to convert to decimal |
Returns
Type: Decimal
A decimal representation with exact precision (28-29 significant digits).
Examples
Currency Conversion
Data:
doc.Params["model"] = new {
price = "19.99"
};
Output:
<p>Total: $19.99</p>
Financial Calculations
Data:
doc.Params["model"] = new {
principal = "1000.00",
rate = "0.05"
};
Output:
<p>Interest: $50.00</p>
Precise Arithmetic
Data:
doc.Params["model"] = new {
price = "9.99",
quantity = "3",
taxRate = "0.08"
};
Output:
<p>Subtotal: $29.97</p>
<p>Tax: $2.40</p>
<p>Total: $32.37</p>
Notes
- Provides exact decimal precision (no floating-point errors)
- Ideal for financial and monetary calculations
- Handles string-to-decimal conversion
- Throws exception if value cannot be converted
- Range: ±1.0 × 10^−28 to ±7.9228 × 10^28
- More precise than
double()but smaller range - Avoids floating-point rounding errors (e.g., 0.1 + 0.2 = 0.3)