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? Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/ 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] Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130586 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] Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130593 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] Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130595 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? Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130596 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(","); ? Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130601 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! Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130602 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] Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130616 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.. Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130628 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 Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130634 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] Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130663 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. Link to comment https://forums.phpfreaks.com/topic/28538-change-a-number-like-250000-into-250k/#findComment-130721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.