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

  • SEARCH
  • Home
  • 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 9129563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:46:44+00:00 2026-06-17T07:46:44+00:00

I would like to work with numbers like 10M (which represents 10 000 000),

  • 0

I would like to work with numbers like 10M (which represents 10 000 000), and 100K etc. Is there a function that already exists in PHP, or do I have to write my own function?

I’m thinking something along the lines of :

echo strtonum("100K"); // prints 100000

Also, taking it one step further and doing the opposite, something to translate and get 100K from 100000?

  • 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-06-17T07:46:45+00:00Added an answer on June 17, 2026 at 7:46 am

    You could whip up your own function, because there isn’t an builtin function for this. To give you an idea:

    function strtonum($string)
    {
        $units = [
            'M' => '1000000',
            'K' => '1000',
        ];
    
        $unit = substr($string, -1);
    
        if (!array_key_exists($unit, $units)) {
            return 'ERROR!';
        }
    
        return (int) $string * $units[$unit];
    }
    

    Demo: http://codepad.viper-7.com/2rxbP8

    Or the other way around:

    function numtostring($num)
    {
        $units = [
            'M' => '1000000',
            'K' => '1000',
        ];
    
        foreach ($units as $unit => $value) {
            if (is_int($num / $value)) {
                return $num / $value . $unit;
            }
        }   
    }
    

    Demo: http://codepad.viper-7.com/VeRGDs


    If you want to get really funky you could put all that in a class and let it decide what conversion to run:

    <?php
    
    class numberMagic
    {
        private $units = [];
    
        public function __construct(array $units)
        {
            $this->units = $units;
        }
    
        public function parse($original)
        {
            if (is_numeric(substr($original, -1))) {
                return $this->numToString($original);
            } else {
                return $this->strToNum($original);
            }
        }
    
        private function strToNum($string)
        {
            $unit = substr($string, -1);
    
            if (!array_key_exists($unit, $this->units)) {
                return 'ERROR!';
            }
    
            return (int) $string * $this->units[$unit];
        }
    
        private function numToString($num)
        {
            foreach ($this->units as $unit => $value) {
                if (is_int($num / $value)) {
                    return $num / $value . $unit;
                }
            }   
        }
    }
    
    $units = [
        'M' => 1000000,
        'K' => 1000,
    ];
    $numberMagic = new NumberMagic($units);
    echo $numberMagic->parse('100K'); // 100000
    echo $numberMagic->parse(100); // 100K
    

    Although this may be a bit overkill 🙂

    Demo: http://codepad.viper-7.com/KZEc7b

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make function which detects/validates that a string got at least
I would like to create a function that returns a vector of numbers a
I'm trying to make some input fields that I would like to work like
Does the replication system that comes with DB4O work well? Basically I would like
I have a list of numbers that i would like to send out onto
I have a 2D array that I would like to fill with numbers, one
I would like help finishing my function that compares two arrays and returns the
I would like to work out a number's factorial. My factorial rule is in
I would like to work with internal .xlsx files (styles.xml). I was using <oXygen/>
i would like to work on a website in order to develop other fonctionalities.

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.