|
|
money_format (PHP 4 >= 4.3.0, PHP 5) money_format -- Форматирует число как денежную величину Описаниеstring money_format ( string format, float number )
money_format() форматирует число
number как денежную величину.
Эта функция вызывает функцию strfmon языка C, но позволяет
преобразовать только одно число за один вызов.
Замечание:
Функция money_format() определена только если
в системе присутствует функция strfmon. Например, в Windows она
отсутствует, поэтому money_format() не определена
в Windows.
Описание формата состоит из:
символа % необязательных флагов необязательной ширины поля необязательной точности до запятой необязательной точности после запятой обязательного описателя преобразования
Замечание:
На работу этой функции влияет установка категории
LC_MONETARY текущей локали.
Перед использованием этой функции установите нужную локаль с помощью
setlocale().
Символы перед и после описания формата возвращаются без изменений.
Пример 1. Пример использования money_format()
Проиллюстрируем применение этой функции для различных локалей и
разных описаний формата.
|
<?php
$number = 1234.56;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
$number = -1234.5672;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
echo money_format('%=*(#10.2n', $number) . "\n";
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
setlocale(LC_MONETARY, 'en_GB');
$fmt = 'The final value is %i (after a 10%% discount)';
echo money_format($fmt, 1234.56) . "\n";
?>
|
|
См. также описание функций setlocale(),
number_format(),sprintf(),
printf() и sscanf().
scot from ezyauctionz.co.nz
06-Oct-2007 03:10
This is a handy little bit of code I just wrote, as I was not able to find anything else suitable for my situation.
This will handle monetary values that are passed to the script by a user, to reformat any comma use so that it is not broken when it passes through an input validation system that checks for a float.
It is not foolproof, but will handle the common input as most users would input it, such as 1,234,567 (outputs 1234567) or 1,234.00 (outputs 1234.00), even handles 12,34 (outputs 12.34), I expect it would work with negative numbers, but have not tested it, as it is not used for that in my situation.
This worked when other options such as money_format() were not suitable or possible.
===============
///////////////
// BEGIN CODE convert all price amounts into well formatted values
function converttonum($convertnum,$fieldinput){
$bits = explode(",",$convertnum); // split input value up to allow checking
$first = strlen($bits[0]); // gets part before first comma (thousands/millions)
$last = strlen($bits[1]); // gets part after first comma (thousands (or decimals if incorrectly used by user)
if ($last <3){ // checks for comma being used as decimal place
$convertnum = str_replace(",",".",$convertnum);
}
else{ // assume comma is a thousands seperator, so remove it
$convertnum = str_replace(",","",$convertnum);
}
$_POST[$fieldinput] = $convertnum; // redefine the vlaue of the variable, to be the new corrected one
}
@converttonum($_POST[inputone],"inputone");
@converttonum($_POST[inputtwo],"inputtwo");
@converttonum($_POST[inputthree],"inputthree");
// END CODE
//////////////
================
This is suitable for the English usage, it may need tweaking to work with other types.
winkjr at sound-o-mat dot com
28-Jul-2007 06:42
Agreed, be sure to check that money_format() is defined at all for your version of PHP. I have PHP 4.4.5 w/dev. packages built from source tarballs and it's not defined. I think the docs are wrong, and it's only available in PHP 5.x.
richard dot selby at uk dot clara dot net
17-Feb-2006 07:02
Double check that money_format() is defined on any version of PHP you plan your code to run on. You might be surprised.
For example, it worked on my Linux box where I code, but not on servers running BSD 4.11 variants. (This is presumably because strfmon is not defined - see note at the top of teis page). It's not just a windows/unix issue.
|