Skip to main content Link Search Menu Expand Document (external link)

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

<p>Price: </p>
<!-- Output: Price: $19.99 -->

Data:

doc.Params["model"] = new { price = 19.99m };

Number Formatting

<p>Population: </p>
<!-- Output: Population: 1,234,567 -->

Percentage Formatting

<p>Growth: </p>
<!-- Output: Growth: 15.6% -->

Data:

doc.Params["model"] = new { growth = 0.156 };

Date Formatting

<p>Date: </p>
<!-- Output: Date: 2024-03-15 -->

<p>Published: </p>
<!-- Output: Published: March 15, 2024 -->

With Calculations

<p>Total: </p>

<p>Complete: </p>

Notes

  • If format parameter is omitted, uses default .ToString() conversion
  • Format strings are culture-sensitive
  • Invalid format strings may throw exceptions
  • Use with ?? operator for safety: ``

See Also