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 8869765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:33:31+00:00 2026-06-14T17:33:31+00:00

For an online calculator where users may enter an energy amount to calculate corresponding

  • 0

For an online calculator where users may enter an energy amount to calculate corresponding fees, I need the PHP script to accept various user inputs. The value of “2 million and one fourth joule” may be entered as:

2000000.25 (default notation)

2,000,000.25 (with thousands separator)

2000000,25 (comma as decimal point)

2.000.000,25 (comma as decimal point, with thousands separator)

2’000’000.25 (alternative format)

2 000 000,25 (French notation)

How could I make the script aware of such differences?

My first try was to just str_replace alternative characters with the default ones, but the period (.) may be either a decimal or a thousands separator. I tried using sscanf but how can I make sure that it reads the number correctly?

Most users will only provide two digits after the decimal point, but is there any way I can distinguish 1.234 (1 point 234, period as decimal separator) and 1.234 (one thousand two hundred thirty-four, period as thousands separator)?

  • 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-14T17:33:33+00:00Added an answer on June 14, 2026 at 5:33 pm

    Since I wasn’t able to find a simple solution via some built-in PHP functions, I wrote two functions to (1) check if the entered string may be a number at all and (2) if it is well-formed depending on the separators used.

    I restricted the possible separators to period (.), comma (,), space () and apostrophe (') as thousands separators. The decimal point may only be one of the first two options. Both sets of separators can be edited to allow even more or restrict the ones in place.

    What I am actually doing is to look for all number columns and all separators by using a couple of simple preg_match_all calls.

    The complete code reads as follows and should be self-explaining as I added some comments when throwing a false. I’m sure, this can be simplified somehow, but it works right now and filters many errors while allowing even some strange combinations such as 2 000 000.25 or 2'000'000,25.

        function check_number($number) {
            if ((int) substr($number,0,1) == 0) {
                return false; // not starting with a digit greater than 0
            }
            if ((string) substr($number,-1) != "0" && (int) substr($number,-1) == 0) {
                return false; // not ending with a digit
            }
            preg_match_all('/([^0-9]{2,})/', $number, $sep, PREG_PATTERN_ORDER);
            if (isset($sep[0][0])) {
                return false; // more than one consecutive non-digit character
            }
            preg_match_all('/([^0-9]{1})/', $number, $sep, PREG_PATTERN_ORDER);
            if (count($sep[0]) > 2 && count(array_unique($sep[0])) > 2) {
                return false; // more than 2 different separators
            }
            elseif (count($sep[0]) > 2) {
                $last_sep = array_pop($sep[0]);
                if (!in_array($last_sep,array(".",","))) {
                    return false; // separator not allowed as last one
                }
                $sep_unique = array_unique($sep[0]);
                if (count($sep_unique) > 1) {
                    return false; // not all separators (except last one) are identical 
                }
                elseif (!in_array($sep_unique[0],array("'",".",","," "))) {
                    return false; // separator not allowed
                }
            }
            return true;
        }
    
        function convert_number($number) {
            preg_match_all('/([0-9]+)/', $number, $num, PREG_PATTERN_ORDER);
            preg_match_all('/([^0-9]{1})/', $number, $sep, PREG_PATTERN_ORDER);
            if (count($sep[0]) == 0) {
                // no separator, integer
                return (int) $num[0][0];
            }
            elseif (count($sep[0]) == 1) {
                // one separator, look for last number column
                if (strlen($num[0][1]) == 3) {
                    if (strlen($num[0][0]) <= 3) {
                        // treat as thousands seperator
                        return (int) ($num[0][0] * 1000 + $num[0][1]);
                    }
                    elseif (strlen($num[0][0]) > 3) {
                        // must be decimal point
                        return (float) ($num[0][0] + $num[0][1] / 1000);
                    }
                }
                else {
                    // must be decimal point
                    return (float) ($num[0][0] + $num[0][1] / pow(10,strlen($num[0][1])));
                }
            }
            else {
                // multiple separators, check first an last
                if ($sep[0][0] == end($sep[0])) {
                    // same character, only thousands separators, check well-formed nums
                    $value = 0;
                    foreach($num[0] AS $p => $n) {
                        if ($p == 0 && strlen($n) > 3) {
                            return -1; // malformed number, incorrect thousands grouping
                        }
                        elseif ($p > 0 && strlen($n) != 3) {
                            return -1; // malformed number, incorrect thousands grouping
                        }
                        $value += $n * pow(10, 3 * (count($num[0]) - 1 - $p));
                    }
                    return (int) $value;
                }
                else {
                    // mixed characters, thousands separators and decimal point
                    $decimal_part = array_pop($num[0]);
                    $value = 0;
                    foreach($num[0] AS $p => $n) {
                        if ($p == 0 && strlen($n) > 3) {
                            return -1; // malformed number, incorrect thousands grouping
                        }
                        elseif ($p > 0 && strlen($n) != 3) {
                            return -1; // malformed number, incorrect thousands grouping
                        }
                        $value += $n * pow(10, 3 * (count($num[0]) - 1 - $p));
                    }
                    return (float) ($value + $decimal_part / pow(10,strlen($decimal_part)));
                }
            }
        }
    

    I am aware of one flaw this set of function has: 1.234 or 1,234 will always be treated as the whole number 1234, as the function assumes the separator must be a thousands separator if there are less than 4 digits in front of the single separator.

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

Sidebar

Related Questions

Ok I need some help, I have an online calculator where I have to
Various online services have different values for maximum year of expiry, when it comes
I need some inspiration for a solution... We are running an online game with
I'm trying to create an online calculator as a means to help me learn
I have to build an App that need authentication over a DB (online). When
From this online calculator: http://homer.freeshell.org/dd.cgi using its data I've successfully written a working version,
I've had something strange happen lately with php script I use to send emails
I'm working on an online tool for students in my faculty to calculate their
Would it be possible to calculate time online from a table of something like
I'm noticing that all the MPI calls need some amount of symmetry or they

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.