|
money_format
Formats a number as a currency string
(PHP 4 >= 4.3.0, PHP 5)
Example 2421. money_format() ExampleWe will use different locales and format specifications to illustrate the use of this function. <?php Code Examples / Notes » money_formatscot from ezyauctionz.co.nz
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. www dot spam
For users of Windows looking for basic number formatting such as decimal places, decimal seperator and thousands seperators use number_format() instead. http://www.php.net/number_format richard dot selby
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. winkjr
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.
|
Change Languageaddcslashes addslashes bin2hex chop chr chunk_split convert_cyr_string convert_uudecode convert_uuencode count_chars crc32 crypt echo explode fprintf get_html_translation_table hebrev hebrevc html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode quotemeta rtrim setlocale sha1_file sha1 similar_text soundex sprintf sscanf str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split str_word_count strcasecmp strchr strcmp strcoll strcspn strip_tags stripcslashes stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk strpos strrchr strrev strripos strrpos strspn strstr strtok strtolower strtoupper strtr substr_compare substr_count substr_replace substr trim ucfirst ucwords vfprintf vprintf vsprintf wordwrap |