cairesdesigns Posted January 21, 2007 Share Posted January 21, 2007 I currently am working on a text-based RPG and I need some help with a script that makes abbreviations for the users money, for example if a user has $1,000,000 I would like it to be converted to $1.0 Mil instead etc... If anyone could please help this would be great, or if you already know of functions that are able to do this please let me know! It would be much appreciated. So far this is what I have come up with[code]function abrev($number) { $num = strlen($number); if($num > 18) { print substr($number, 0, -18); print substr($number, -18, strpos($number, '0')); print " Quin"; } elseif($num > 15) { print substr($number, 0, -15); print substr($number, -15, strpos($number, '0')); print " Quad"; } elseif($num > 12) { print substr($number, 0, -12); print substr($number, -12, strpos($number, '0')); print " Til"; } elseif($num > 9) { print substr($number, 0, -9); print substr($number, -9, strpos($number, '0')); print " Bil"; } elseif($num > 6) { print substr($number, 0, -6); print substr($number, -6, strpos($number, '0')); print " Mil"; } else { print number_format(floor($number)); }}[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 What's wrong with that, it's a very good function.I think use it, if it's not doing what you want, make some modifications. Where is your current function failing, or not doing what you want? Quote Link to comment Share on other sites More sharing options...
mattd8752 Posted January 21, 2007 Share Posted January 21, 2007 I would simply str_replace 000,000,000 with M first (from biggest value to least)then 00,000 with HT ect. You could try that, but only run it on the output that should be replaced. Quote Link to comment Share on other sites More sharing options...
cairesdesigns Posted January 21, 2007 Author Share Posted January 21, 2007 Well see what i have works to a small extent, see if i have a really big number, say 9999999999. it will display 9,999999999Tril instead of lets say 9.9Tril Quote Link to comment Share on other sites More sharing options...
448191 Posted January 21, 2007 Share Posted January 21, 2007 http://nl2.php.net/round Quote Link to comment Share on other sites More sharing options...
cairesdesigns Posted January 21, 2007 Author Share Posted January 21, 2007 Could you take my code that i have and add that into it, I couldn't get it to work Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 <?phpfunction abrev($number) { $num = strlen($number); if($num > 18) { print round($number); print substr($number, -18, strpos($number, '0')); print " Quin"; } elseif($num > 15) { print round($number); print substr($number, -15, strpos($number, '0')); print " Quad"; } elseif($num > 12) { print round($number); print substr($number, -12, strpos($number, '0')); print " Til"; } elseif($num > 9) { print round($number); print substr($number, -9, strpos($number, '0')); print " Bil"; } elseif($num > 6) { print round($number); print substr($number, -6, strpos($number, '0')); print " Mil"; } else { print number_format(floor($number)); }}?> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 Really tired, up late trying to answer questions, so that may have been a pathetic attempt. Quote Link to comment Share on other sites More sharing options...
cairesdesigns Posted January 21, 2007 Author Share Posted January 21, 2007 Ok well say i have this number "63,961,764,106,441" the script puts out this "$6.39617641064E+13961764106 Til" something is definitly wrong there Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 [code]<?phpfunction abrev($number) { // get length $num = strlen($number); if($num > 18) { // if 18 characters long print round($number); // round it print " Quin"; // print type (whatever it's called } elseif($num > 15) { // if 15 characters print round($number); // print rounded number print " Quad"; // print type } elseif($num > 12) { // 12 characters print round($number); // round and print number print " Til"; // followed by type } elseif($num > 9) { print round($number); print " Bil"; } elseif($num > 6) { print round($number); print " Mil"; } else { print number_format(floor($number)); }}?>[/code] Quote Link to comment Share on other sites More sharing options...
448191 Posted January 21, 2007 Share Posted January 21, 2007 [code=php:0]if($num > 18) { print round(substr($number, 0, -18).substr($number, -18, strpos($number, '0')),$round); print " Quin";}[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 Very nice 448191 Quote Link to comment Share on other sites More sharing options...
448191 Posted January 21, 2007 Share Posted January 21, 2007 It didn't feel right to use string functions on floats, and, sure enough: it doesn't work.Once you get values that include 'E', using string functions produces unusable results.Below code is tested and works fine on values between 10[sup]3[/sup](1000) and 10[sup]18[/sup].[code]<?phpfunction abbrev($float, $round = 1) { $map = array('Thou', 'Mil', 'Bil', 'Til', 'Quad', 'Quin'); $x = $float; for($i = 0; true; $i++){ if($x / 1000 < 1000){ break; } $x = $x / 1000; } $index = round($i,0); echo round($float/( pow(1000,$index+1) ),$round).' '.$map[$index]; }?>[/code] Quote Link to comment Share on other sites More sharing options...
448191 Posted January 21, 2007 Share Posted January 21, 2007 I made it a little more flexable. Keep in mind that I'm [u]not[/u] exactly an expert on math.. ::)[code]<?phpfunction get_crude_exponent($float, $base, $highBaseErrLvl = E_USER_NOTICE){ if($float < $base){ trigger_error('get_crude_exponent(): Float "'.$float.'" smaller then base "'.$base.'".',$highBaseErrLvl); } $x = $float; for($e = 1; true; $e++){ if($x / $base < $base){ return $e; } $x = $x / $base; } }function qualified_float($qual, $base, $float, $precision = 1) { if(is_array($float)){ foreach($float as $float2){ $arr[] = qualified_float($qual, $base, $float2, $precision); } return $arr; } $e = get_crude_exponent($float, $base); if(!isset($qual[$e-1])){ $maxQualIndex = max(array_keys($qual)); return round($float/(pow($base,$maxQualIndex+1)),$precision).' '.$qual[$maxQualIndex]; } else { return round($float/(pow($base,$e)),$precision).' '.$qual[$e-1]; } return false;}//Test$qualifiers = array('Tens', 'Hundred', 'Thousand', 'Ten thousand', 'Hundred thousand', 'Million');$qualified = qualified_float($qualifiers, 10, array(1,12,123,1234,12456,123456,1234567,12345678,123456789));echo '<pre>';print_r($qualified);echo '</pre>';$qualifiers = array('Thou', 'Mil', 'Bil', 'Til', 'Quad', 'Quin');$qualified = qualified_float($qualifiers, 1000, array(1234,12345,123456,1234567,12345678,123456789),3);echo '<pre>';print_r($qualified);echo '</pre>';echo qualified_float($qualifiers, 1000, 1234567891,3).'<br/>';echo qualified_float($qualifiers, 1000, 12345678912,3).'<br/>';echo qualified_float($qualifiers, 1000, 123456789123,3).'<br/>';$qualified = qualified_float($qualifiers, 1000, array(1234567891234,12345678912345,123456789123456,1234567891234567 ,12345678912345678,123456789123456789,1234567891234567891,12345678912345678912,123456789123456789123),4);echo '<pre>';print_r($qualified);echo '</pre>';?>[/code]Test output:[code]Array( [0] => 0.1 Tens [1] => 1.2 Tens [2] => 1.2 Hundred [3] => 1.2 Thousand [4] => 1.2 Ten thousand [5] => 1.2 Hundred thousand [6] => 1.2 Million [7] => 12.3 Million [8] => 123.5 Million)Array( [0] => 1.234 Thou [1] => 12.345 Thou [2] => 123.456 Thou [3] => 1.235 Mil [4] => 12.346 Mil [5] => 123.457 Mil)1.235 Bil12.346 Bil123.457 BilArray( [0] => 1.2346 Til [1] => 12.3457 Til [2] => 123.4568 Til [3] => 1.2346 Quad [4] => 12.3457 Quad [5] => 123.4568 Quad [6] => 1.2346 Quin [7] => 12.3457 Quin [8] => 123.4568 Quin)[/code] 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.