2023-02-06 14:42:31 +01:00
|
|
|
# string_format
|
2021-12-03 11:59:22 +01:00
|
|
|
|
|
|
|
This is a way to format strings, such as decimal numbers and such. Use
|
2023-02-03 22:31:59 +01:00
|
|
|
the syntax for [`sprintf()`](https://www.php.net/sprintf) for the
|
2021-12-03 11:59:22 +01:00
|
|
|
formatting.
|
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
## Basic usage
|
|
|
|
```smarty
|
|
|
|
{$myVar|string_format:"%d"}
|
|
|
|
```
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
## Parameters
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
| Parameter Position | Type | Required | Description |
|
|
|
|
|--------------------|--------|----------|---------------------------------------|
|
|
|
|
| 1 | string | Yes | This is what format to use. (sprintf) |
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
## Examples
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
```php
|
|
|
|
<?php
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
$smarty->assign('number', 23.5787446);
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
```
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
Where template is:
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
```smarty
|
|
|
|
{$number}
|
|
|
|
{$number|string_format:"%.2f"}
|
|
|
|
{$number|string_format:"%d"}
|
|
|
|
```
|
2021-12-03 11:59:22 +01:00
|
|
|
|
|
|
|
Will output:
|
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
```
|
|
|
|
23.5787446
|
|
|
|
23.58
|
|
|
|
23
|
|
|
|
```
|
2021-12-03 11:59:22 +01:00
|
|
|
|
2023-02-06 14:42:31 +01:00
|
|
|
See also [`date_format`](language-modifier-date-format.md).
|