توابع money_format() در PHP

مثال

فرمت بین‌المللی en_US:

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("قیمت USD %i", $number);
?>

The output of the above code:

قیمت USD 1,234.56 است

تعریف و استفاده

توابع money_format() یک رشته است که به عنوان رشته پولی فرمت شده بازمی‌گردد.

این تابع یک عدد فرمت شده را در موقعیت درصد (%) رشته اصلی قرار می‌دهد.

Note:توابع money_format() نمی‌توانند در سیستم عامل ویندوز کار کنند.

نکته:این تابع معمولاً با setlocale() با هم استفاده شود.

نکته:برای مشاهده تمام کد‌های زبان موجود، لطفاً بهدستورالعمل کد زبان

منطق

money_format(string,number)
پارامتر توضیح
string

لازم. تعریف رشته‌ای که باید فرمت شود و نحوه فرمت‌دهی متغیرهای موجود در آن.

ممکن است مقادیر فرمت:

پرکننده و علامت‌ها:

  • =f - تعریف کاراکتر (f) به عنوان پرکننده (مثلاً %=t از "t" به عنوان پرکننده استفاده می‌شود). پیش‌فرض استفاده از فضای خالی به عنوان پرکننده است.
  • ^ - حذف استفاده از کاراکترهای گروه‌بندی.
  • + یا ( - تعریف نحوه نمایش اعداد مثبت و منفی. اگر از "+" استفاده شود، از + و - تنظیم شده توسط محیط (معمولاً علامت به قبل از اعداد منفی اضافه می‌شود و هیچ علامتی به قبل از اعداد مثبت اضافه نمی‌شود) استفاده می‌شود. اگر از ")" استفاده شود، اعداد منفی در داخل گوشه‌ها قرار می‌گیرند. پیش‌فرض استفاده از "+" است.
  • ! - توقف استفاده از علامت پولی در رشته خروجی.
  • - اگر از "-" استفاده شود، تمام فیلدها به چپ جابجا می‌شوند. پیش‌فرض راست‌چین است.

عرض فیلد:

  • x - تعریف عرض کمینه فیلد (x). پیش‌فرض 0 است.
  • #x - تعریف تعداد بیشترین دفعات اعداد سمت چپ از داگم (x). برای نگهداری از چیدمان فرمت شده در یک ستون مرتب است. اگر تعداد دفعات اعداد بیشتر از x باشد، این تعریف نادیده گرفته خواهد شد.
  • .x - Specifies the maximum number of digits to the right of the decimal point (x). If x is 0, the decimal point and the digits to the right will not be displayed. The default is to use the local settings.

Conversion Characters:

  • i - The number is formatted as the international currency format.
  • n - The number is formatted as the national currency format.
  • % - Returns the % character.

Note:If multiple format values are used, they must appear in the order above.

Note:This function is affected by the local settings.

number Required. The number inserted into the % symbol position in the formatted string.

Technical Details

Return value:

Returns the formatted string.

Characters before and after the formatted string will remain unchanged and returned. Non-numeric numbers will return NULL and produce an E_WARNING.

PHP Version: 4.3.0+

More Examples

Example 1

International format with 2 decimal places (Germany):

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

The output of the above code:

1 234,56 EUR

Example 2

Negative numbers, with parentheses () indicating negative numbers in the US international format, right precision of 2, and "*" as the fill character:

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

The output of the above code:

(******1234.57)