Customizing the precision in percent formatting in Sonata IntlBundle

By default, the number_format_percent macro in the Sonata IntlBundle formats numbers without a decimal fraction. For example, if the value passed to it is .1240, the resulting formatted string will be “12%”. If you want to add additional levels of precision, you can customize it with the fraction_digits argument like this:

{{ .1240|number_format_percent({'fraction_digits': 2}) }}

The resulting formatted string will be “12.40%”. This isn’t documented very well, and is a source of frustration if you want to add more precision.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s