Home > Backend Development > PHP Tutorial > Parsing the money_format () function in PHP

Parsing the money_format () function in PHP

autoload
Release: 2023-04-09 22:32:01
Original
5410 people have browsed it

In PHP, we often need to format numbers into currency strings. In this case, we need to use PHP's built-in function money_format() function. This article will take you to take a look at this function. First, let's take a look at the syntax of this function:

money_format ( string $format , float $number )
Copy after login
  • $format: Specifies the string to be formatted and how to format the variables in the string. Single % character optional flags optional field width optional, left precision optional, right precision required, single conversion character.

  • $number: The number that needs to be formatted.

  • Return value: Returns the formatted characters. Characters before and after the format string will be returned unchanged. If the number passed in is not a number, null will be returned and E_WARNING will be generated.

Note: money_format() function This function wraps strfmon() in the C function library, only It is defined that it can only be used when the system running this function has the strfmon function, but the Windows system does not. All money_format() is available in Windows is not defined, resulting in an undefined error. Or the php version you are using is lower than 4.3.

1. The use of positive numbers

<?php
$number = 1234.56;
// 打印 en_US locale 的国际化格式
setlocale(LC_MONETARY, &#39;en_US&#39;);
echo money_format(&#39;%i&#39;, $number) . "\n";
// USD 1,234.56

// 打印意大利国家的格式,带两位浮点小数`
setlocale(LC_MONETARY, &#39;it_IT&#39;);
echo money_format(&#39;%.2n&#39;, $number) . "\n";
// Eu 1.234,56
?>
Copy after login

2. The use of negative numbers

<?php
$number = -1234.5672;
// 美国国家的格式,使用圆括号 () 标记负数。
// 左侧精度使用十位
setlocale(LC_MONETARY, &#39;en_US&#39;);
echo money_format(&#39;%(#10n&#39;, $number) . "\n";
// ($        1,234.57)

// 相似的格式,添加了右侧两位小数点的精度,同时用 * 来填充
echo money_format(&#39;%=*(#10.2n&#39;, $number) . "\n";
// ($********1,234.57)
Copy after login

3. Add some introduction before and after the format string

<?php
setlocale(LC_MONETARY, &#39;en_GB&#39;);
$fmt = &#39;The final value is %i (after a 10%% discount)&#39;;
echo money_format($fmt, 1234.56) . "\n";
// The final value is  GBP 1,234.56 (after a 10% discount)
Copy after login

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Parsing the money_format () function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template