jwk811 Posted November 26, 2006 Share Posted November 26, 2006 is there a way i could change big numbers into a number ending in K instead of 250,000, 250K. or 1,000,000, 1MILis this possible? Quote Link to comment Share on other sites More sharing options...
fert Posted November 26, 2006 Share Posted November 26, 2006 [code]$num=str_replace(",","",$num);$num=str_replace("000000","M",$num);$num=str_replace("000","K",$num);[/code] Quote Link to comment Share on other sites More sharing options...
mainewoods Posted November 26, 2006 Share Posted November 26, 2006 if you number does not have commas in it:[code]<?php$ext = ''; //initializeif (($num / 1000000) >= 1) { //test for millions $num = $num / 1000000; $ext = 'MIL';} else if (($num / 1000) >= 1) { //test for thousands $num = $num / 1000; $ext = 'K';} echo number_format($num) . $ext; //number_format() adds commas?>[/code] Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 26, 2006 Share Posted November 26, 2006 Here´s another way:ex[code=php:0]<?php $number = "250,000";list($money, $type) = explode(',', $number, 2); if ($type == "000") $money .= "K"; if ($type == "000,000") $money .= "MIL";echo $money;?>[/code] Quote Link to comment Share on other sites More sharing options...
jwk811 Posted November 26, 2006 Author Share Posted November 26, 2006 thanks! they all work great but theres one problem.. when i put in something like 1,250,000 it doesnt come out right.. would you know how i could do this? Quote Link to comment Share on other sites More sharing options...
jwk811 Posted November 26, 2006 Author Share Posted November 26, 2006 so to get rid of commas all i would have to do it do explode(","); ? Quote Link to comment Share on other sites More sharing options...
jwk811 Posted November 26, 2006 Author Share Posted November 26, 2006 could i have it so if they put it in in either with commas or not it will still work? and also the decimal thing. thanks! Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Share Posted November 26, 2006 here ya go... i just made this for ya...[code]<?function bignumbers($i){ if(strlen($i)<=3) return $i; $a=array('','K','Mil'); $i=explode(",", $i); $c=count($i); return $i[0].$a[$c-1];}echo bignumbers('1,000,000');echo bignumbers('1,000');echo bignumbers('1');?>[/code] Quote Link to comment Share on other sites More sharing options...
jwk811 Posted November 26, 2006 Author Share Posted November 26, 2006 thanks but what about the comma thing? when i call the functon with a number that doesnt have the comma it just comes up as is.. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 26, 2006 Share Posted November 26, 2006 If you have, as in your example, a value of 1,250,000, do you want to show that as1.25Mor1,250K Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Share Posted November 26, 2006 [code]function bignumbers($i){ if(strlen($i)<=3) return $i; if(strpos(",",$i)===false) $i=number_format($i); $a=array('','K','M'); $i=explode(",", $i); $c=count($i); return $i[0].$a[$c-1];}echo bignumbers('100000');echo bignumbers('100,000');[/code] Quote Link to comment Share on other sites More sharing options...
jwk811 Posted November 26, 2006 Author Share Posted November 26, 2006 yes for the decimal part i want it to come out as 1.25MIL if someone typed in 1,250,000.. and taith with the new code you gave its missing the end (K or MIL) for the number [b]with[/b] the commas.. and the other one is now coming out fine, lol. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.