Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3975006
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:38:03+00:00 2026-05-20T04:38:03+00:00

my question is: is there a good (common) algorithm to create numbers, which match

  • 0

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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:38:04+00:00Added an answer on May 20, 2026 at 4:38 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the question states - is there a good tool out there which lists
Following this question: Good crash reporting library in c# Is there any library like
I'm sure this is a relatively simple question, and there must be a good
There are many skills a programmer could have (understanding the problem, asking good questions,
Question Is there a way to have a method that will always run anytime
Question: Is there an easy way (library function) to perform a bitwise AND or
We have the question is there a performance difference between i++ and ++i in
Or to reformulate the question: is there a performance penalty in using unsigned values?
I have seen this question: Are there any decent UI components for touch screen
Since I've gotten no answer at all to my question Is there an alternative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.