how to create "pretty" numbers?

1.9k Views Asked by At

my question is: is there a good (common) algorithm to create numbers, which match well looking user understood numbers out of incomming (kind of random looking for a user) numbers.

i.e. you have an interval from

130'777.12 - 542'441.17.

But for the user you want to display something more ...say userfriendly, like:

130'000 - 550'000.

how can you do this for several dimensions? an other example would be:

23.07 - 103.50 to 20 - 150

do you understand what i mean?

i should give some criteria as well:

  • the interval min and max should include the given limits.
  • the "rounding" should be in a granularity which reflects the distance between min and max (meaning in our second example 20 - 200 would be too coarse)

very much honor you'll earn if you know a native php function which can do this :-)

*update - 2011-02-21 *

I like the answer from @Ivan and so accepted it. Here is my solution so far:

maybe you can do it better. i am open for any proposals ;-).

/**
 * formats a given float number to a well readable number for human beings
 * @author helle + ivan + greg
 * @param float $number 
 * @param boolean $min regulates wheter its the min or max of an interval
 * @return integer
 */
function pretty_number($number, $min){
    $orig = $number;
    $digit_count = floor(log($number,10))+1; //capture count of digits in number (ignoring decimals)
    switch($digit_count){
        case 0: $number = 0; break;
        case 1:
        case 2: $number = round($number/10) * 10; break;
        default: $number = round($number, (-1*($digit_count -2 )) ); break;
    }

    //be sure to include the interval borders
    if($min == true && $number > $orig){
        return pretty_number($orig - pow(10, $digit_count-2)/2, true);
    }

    if($min == false && $number < $orig){
        return pretty_number($orig + pow(10, $digit_count-2)/2, false);
    }

    return $number;

}
5

There are 5 best solutions below

4
On BEST ANSWER

I would use Log10 to find how "long" the number is and then round it up or down. Here's a quick and dirty example.

echo prettyFloor(23.07);//20
echo " - ";
echo prettyCeil(103.50);//110

echo prettyFloor(130777.12);//130000
echo " - ";
echo prettyCeil(542441.17);//550000

function prettyFloor($n)
{
  $l = floor(log(abs($n),10))-1; // $l = how many digits we will have to nullify :)
  if ($l<=0)
    $l++;

  if ($l>0)
    $n=$n/(pow(10,$l)); //moving decimal point $l positions to the left eg(if $l=2 1234 => 12.34 )
  $n=floor($n);
  if ($l>0)
    $n=$n*(pow(10,$l)); //moving decimal point $l positions to the right eg(if $l=2 12.3 => 1230 )
  return $n;
}

function prettyCeil($n)
{
  $l = floor(log(abs($n),10))-1;
  if ($l<=0)
    $l++;
  if ($l>0)
    $n=$n/(pow(10,$l));
  $n=ceil($n);
  if ($l>0)
    $n=$n*(pow(10,$l));
  return $n;
}

This example unfortunately will not convert 130 to 150. As both 130 and 150 have the same precision. Even thou for us, humans 150 looks a bit "rounder". In order to achieve such result I would recommend to use quinary system instead of decimal.

0
On

I haven't seen ready algorithm or function for that. But it should be simple, based on string replacement (str_replace, preg_replace), number_format and round functions.

2
On

You can use php's round function which takes a parameter to specify the precision.

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.05
echo round(5.055, 2);    // 5.06
?>
2
On

The number_format() function handles "prettifying" numbers with arbitrary thousands/decimal characters and decimal places, but you'd have to split your ranges/strings into individual numbers, as number_formation only works on one number at a time.

The rounding portion would have to handled seperately as well.

0
On

This actually is kind of a special case, that can be addressed with the following function:

function roundto($val, $toceil=false) { 
    $precision=2; // try 1, 2, 5, 10 
    $pow = floor(log($val, 10)); 
    $mult = pow(10, $pow);
    $a = $val/$mult*$precision; 
    if (!$toceil) $a-=0.5; else $a+=0.5; 
    return round($a)/$precision*$mult; 
}

$v0=130777.12; $v1=542441.17; 
echo number_format(roundto($v0, false), 0, '.', "'").' - '
    .number_format(roundto($v1, true), 0, '.', "'").'<br/>';

$v0=23.07; $v1=103.50; 
echo number_format(roundto($v0, false), 0, '.', "'").' - '
    .number_format(roundto($v1, true), 0, '.', "'").'<br/>';

Outputs exactly this:

100'000 - 550'000
20 - 150

For any other case of number formatting it might be interesting to have a look at my newly published PHP class "php-beautiful-numbers", which I use in almost ever project to display run times ("98.4 µs" [= 9.8437291615846E-5]) or numbers in running text (e.g. "you booked two flights." [= 2]).

https://github.com/SirDagen/php-beautiful-numbers