Jump to content


Photo

PHP number function


  • Please log in to reply
3 replies to this topic

#1 evertonian7uk

evertonian7uk

    Young Padawan

  • Members
  • Pip
  • 249 posts

Posted 05 December 2006 - 10:38 AM

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.

#2 evertonian7uk

evertonian7uk

    Young Padawan

  • Members
  • Pip
  • 249 posts

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:

str_replace('000,000,000', ' HM', $var['price']);



#3 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

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

#4 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

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.

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. :mellow:

Edited by Demonslay, 05 December 2006 - 05:39 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users