@@ -152,8 +152,32 @@ function number_to_amount($num, int $precision, string $locale = null)
152152
153153//--------------------------------------------------------------------
154154
155+ if (! function_exists ('number_to_currency ' ))
156+ {
157+ function number_to_currency ($ num , string $ currency = null , string $ locale = null )
158+ {
159+ return format_number ($ num , 1 , $ locale , [
160+ 'type ' => NumberFormatter::CURRENCY ,
161+ 'currency ' => $ currency
162+ ]);
163+ }
164+ }
165+
166+ //--------------------------------------------------------------------
167+
155168if (! function_exists ('format_number ' ))
156169{
170+ /**
171+ * A general purpose, locale-aware, number_format method.
172+ * Used by all of the functions of the number_helper.
173+ *
174+ * @param $num
175+ * @param int $precision
176+ * @param string|null $locale
177+ * @param array $options
178+ *
179+ * @return string
180+ */
157181 function format_number ($ num , int $ precision = 1 , string $ locale = null , array $ options =[])
158182 {
159183 // Locale is either passed in here, negotiated with client, or grabbed from our config file.
@@ -169,10 +193,17 @@ function format_number($num, int $precision = 1, string $locale = null, array $o
169193 $ pattern = '#,##0. ' . str_repeat ('# ' , $ precision );
170194
171195 $ formatter = new NumberFormatter ($ locale , $ type );
172- $ formatter ->setPattern ($ pattern );
173196
174197 // Try to format it per the locale
175- $ output = $ formatter ->format ($ num );
198+ if ($ type == NumberFormatter::CURRENCY )
199+ {
200+ $ output = $ formatter ->formatCurrency ($ num , $ options ['currency ' ]);
201+ }
202+ else
203+ {
204+ $ formatter ->setPattern ($ pattern );
205+ $ output = $ formatter ->format ($ num );
206+ }
176207
177208 // This might lead a trailing period if $precision == 0
178209 $ output = trim ($ output , '. ' );
0 commit comments