Home >Backend Development >PHP Problem >How to use php money_format function

How to use php money_format function

藏色散人
藏色散人Original
2019-05-26 11:18:312831browse

php money_format function is used to return a string formatted as a currency string. Its syntax is money_format(string,number). The parameter string is required and refers to the string to be formatted and how to format the variables in it.

How to use php money_format function

How to use the php money_format function?

Definition and Usage

The money_format() function returns a string formatted as a currency string.

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

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

Tips: 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)

How to use php money_format function

Return value:

Return formatted characters string.

The characters before and after the format string will be returned unchanged. Non-numeric numbers return NULL and generate E_WARNING.

PHP Version: 4.3.0

Example 1

International format with 2 decimals (Germany):

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

Output of the above code:

1 234,56 EUR

Example 2

Negative numbers, with () indicating the US international format for negative numbers, the right-hand side precision is 2, and "*" is Fill character:

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

Output of the above code:

(******1234.57)

Example 3

en_US International format:

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

Output of the above code:

The price is USD 1,234.56

The above is the detailed content of How to use php money_format function. 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