Hi guys,
I was searching around for help with PHP number functions,
what i want is on my site, i have people who have great amounts of money, what i am hoping to do is change the number functions:
right now, it displays as $10,000,000 if they have 10 million, what i would like to do is replace the 0's with M so it shows as $10M
is this possible using number funcs? if so can you point me to a tut or give me a code snippet to work from.
as always thanks for any help you can provide!!
Eve.
PHP number function
Started by
evertonian7uk
, Dec 05 2006 10:38 AM
3 replies to this topic
#1
Posted 05 December 2006 - 10:38 AM
#2
Posted 05 December 2006 - 11:10 AM
sweet, and if there over 10 mill lets say hundred million i could just add the extra 0's
like so:
like so:
str_replace('000,000,000', ' HM', $var['price']);
#3
Posted 05 December 2006 - 12:42 PM
there is the option of preg_replace, but generally they take preg stuff takes a bit longer but I prefer preg stuff as you can use regex to make the searches better, but what adam said would work fine.
If you will be using it quite a few times you should write a simple function which you can just pass the value and it returns what you want.
Matt
If you will be using it quite a few times you should write a simple function which you can just pass the value and it returns what you want.
Matt
#4
Posted 05 December 2006 - 05:36 PM
Let's see, something like this should work, since I just tested it.
Based off a similar function I once found for converting filesizes to readable output, with some tweaks.
Accepts any number format and strings (thus the stripping of commas, simply add an array of things to the str_replace() to strip anything else).
Some examples.
Hope that helps you.
Based off a similar function I once found for converting filesizes to readable output, with some tweaks.
function read_money($money, $decimals = 2){ // Strip all commas to ensure number is cast to integer correctly $money = str_replace(',', '', $money); // Variables $i = 0; $names = array('Hundred', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion', 'Quintillion'); // And so on.. // Loop through and divide by a thousand each time through to determine the name while(($money / 1000) > 1){ $money = $money/1000; $i++; } // Return money with the number name return '$'.substr($money, 0, strpos($money, '.')+($decimals+1)).' '.$names[$i]; }
Accepts any number format and strings (thus the stripping of commas, simply add an array of things to the str_replace() to strip anything else).
Some examples.
echo read_money(10000000); // Outputs '$10 Million' echo read_money('23,000,000,000'); // Outputs '$23 Billion' # Also accepts parameter to show decimals echo read_money('54,212,000', 2); // Outputs '$54.21 Million' echo read_money(123456789, 4); // Outputs '$123.4567 Million'
Hope that helps you.
Edited by Demonslay, 05 December 2006 - 05:39 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users