Home  >  Article  >  Backend Development  >  php returns string function money_format() formatted as currency string

php returns string function money_format() formatted as currency string

黄舟
黄舟Original
2017-11-02 13:36:262406browse

Example

en_US International format:

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>

The above code will output:

The price is USD 1,234.56

Definition and usage

money_format() function returns formatted A string that is a currency string.

This function inserts a formatted number at the percent sign (%) position of the main string.

Note: The money_format() function does not work on Windows platforms.

Tip: This function is often used together with the setlocale() function.

Tip: To see all available language codes, visit our Language Codes Reference Manual.

Syntax

money_format(string,number)
Parameters Description
string Required. Specifies the string to be formatted and how to format the variables in the string.

Possible format values:

Padding and flags:

  • =f - specifies the character (f) to be used as padding (for example: %=t use "t " as padding). By default, spaces are used as padding.

  • ^ - Remove the use of grouping characters.

  • + or ( - specifies how to display positive and negative numbers. If "+" is used, the locally set + and - are used (usually a sign is added before the negative number, no sign before the gift book plus any sign). If "(" is used, negative numbers are enclosed in parentheses. The default is to use "+".

  • ##! - Stop using currency symbols in the output string.

  • - If "-" is used, all fields are left aligned. Default is right aligned.

Field width:

  • x - Specifies the minimum width of the field (x). The default is 0.

  • ##x - Specifies the maximum number of digits to the left of the decimal point (x). Formatted output is aligned in the same column. If the number of digits is greater than x, this specification will be ignored.
  • ##.x - Specifies the maximum number of digits to the right of the decimal point (x). is 0, the decimal point and the digits to its right will not be displayed. By default, the
  • conversion character is used. Formatted in international currency format.

##n - The number is formatted in national currency format

  • #% - Returns the % character ##.

  • #Note: If multiple above format values ​​are used, they must be used in the order above and cannot be disrupted.
  • Note: This function is affected by local settings.

  • ##number

  • Required. The number to be inserted into the formatted string at the position of the % symbol.

##

技术细节

返回值:             返回已格式化的字符串。格式化字符串前面和后面的字符将保持不变返回。非数值数字会返回 NULL 并产生 E_WARNING。    

PHP 版本:         4.3.0+    

更多实例

实例 1

带 2 个小数的国际格式(德国):

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>

上面的代码将输出:

1 234,56 EUR

实例 2

负数,带有 () 指示负数的 US 国际格式,右侧精度为 2,"*" 为填充字符:

<?php
$number = -1234.5672;
echo money_format("%=*(#10.2n",$number);
?>

上面的代码将输出:

(******1234.57)


The above is the detailed content of php returns string function money_format() formatted as currency string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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