Jump to content

Money Abbreviations


cairesdesigns

Recommended Posts

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]
Link to comment
Share on other sites

<?php
function 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));
}
}
?>
Link to comment
Share on other sites

[code]<?php
function 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]
Link to comment
Share on other sites

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]
<?php
function 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]
Link to comment
Share on other sites

I made it a little more flexable. Keep in mind that I'm [u]not[/u] exactly an expert on math.. ::)

[code]<?php
function 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 Bil
12.346 Bil
123.457 Bil

Array
(
    [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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.