format() : Format and Convert Values
On this page
Summary
Convert values to formatted strings using .NET format strings. Supports numeric, currency, percentage, and date formatting.
Signature
format(value, format?)
string(value, format?) // Alias
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
value |
Any | Yes | Value to format |
format |
String | No | .NET format string |
Returns
Type: String
The formatted string representation of the value.
Format Strings
Numeric Formats
| Format | Description | Example Input | Example Output |
|---|---|---|---|
N0 |
Number, no decimals | 1234.56 |
1,235 |
N2 |
Number, 2 decimals | 1234.56 |
1,234.56 |
C0 |
Currency, no decimals | 1234.56 |
$1,235 |
C2 |
Currency, 2 decimals | 1234.56 |
$1,234.56 |
P0 |
Percentage, no decimals | 0.1234 |
12% |
P2 |
Percentage, 2 decimals | 0.1234 |
12.34% |
Date Formats
| Format | Example Output |
|---|---|
yyyy-MM-dd |
2024-03-15 |
MM/dd/yyyy |
03/15/2024 |
MMMM dd, yyyy |
March 15, 2024 |
HH:mm:ss |
14:30:45 |
Examples
Currency Formatting
Data:
doc.Params["model"] = new { price = 19.99m };
Number Formatting
Percentage Formatting
Data:
doc.Params["model"] = new { growth = 0.156 };
Date Formatting
With Calculations
Notes
- If
formatparameter is omitted, uses default.ToString()conversion - Format strings are culture-sensitive
- Invalid format strings may throw exceptions
- Use with
??operator for safety: ``