【问题标题】:PHP: decimal format and mysqlPHP:十进制格式和mysql
【发布时间】:2014-11-02 15:12:30
【问题描述】:

我有一个表单,用户可以写不同的数字,可以是十进制,但格式与 PHP 和 MySQL 的默认格式不同。这是我使用的格式:

1,1(一个点一)- 1.000,90(一千零九十)- 1.000.000,90(一百万零九十)等

现在我有这个功能:

function formato_num($num){
    $num = str_replace('.', '', $num);
    $num = str_replace(',', '.', $num);
    return $num;
}

在这些情况下它可以正常工作,但是如果用户出于某种原因使用点 (.) 写入小数,则它不起作用。

我有这个其他功能,但它不能完全正常工作:

if(strpos($_POST['num'],'.') !== false){
    $decimal = $_POST['num'];
    $split = explode('.', $decimal);
    $l = strlen($split[1]);
    if($l < 3){
        echo str_replace('.', ',', $_POST['num']);
    }else{
        $num = str_replace('.', '', $_POST['num']);
        $num = str_replace(',', '.', $_POST['num']);
        echo $num;
    }   
}

有什么帮助吗?谢谢。

示例:

如果用户写: 1.000(一千)我在 DDBB 上保存为 1000。 1,2(十进制)我保存为 1.1。

问题是如果用户使用点作为千位分隔符和小数。

【问题讨论】:

  • 看看 PHP 正则表达式
  • 如果您打算保存这两个数字,是否有理由不将它们保存为两个不同的整数?
  • 如果用户用点写小数点,那么你就有一个不明确的值。您将如何区分这些值?编辑您的问题并提供一些示例以及您希望它们转换为的内容。
  • 我添加了一些例子。
  • $num = number_format($num,'.',',',2);应该工作而不是所有的 str_replace 和东西???

标签: php mysql format decimal


【解决方案1】:

您可以“轻松”转换同时包含逗号和点的字符串,但特别是如果您希望支持错位的千位分隔符或允许用户输入三位小数时会出现歧义。

这是您可以使用的尽力而为的方法。我对任何误解的数字概不负责!

function stringToNumber($str) {
    $last_comma = strrpos($str, ',');
    $last_dot = strrpos($str, '.');
    if($last_comma === false && $last_dot === false) {
        return $str;
    } elseif($last_comma !== false && $last_dot !== false) {
        if($last_comma < $last_dot) {
            // dot is further to the right
            return str_replace(',', '', $str);
        } else {
            // comma is further to the right
            return str_replace(',', '.', str_replace('.', '', $str));
        }
    } elseif ($last_dot !== false) {
        // No commas. For thousand-separator the following must hold
        if(preg_match('/^[0-9]{1,3}(\\.[0-9]{3})+$/', $str)) {
            // every dot is followed by three digits... lets assume the user wanted them as thousand-separators
            // For a single dot this assumption may be invalid, but we expect the user to use . as thousand-separator...
            return str_replace('.', '', $str);
        } else {
            // We could check here how many dots are used.
            return $str;
        }
    } else {
        // No dots. For thousand-separator the following must hold
        if(preg_match('/^[0-9]{1,3}(,[0-9]{3})+,[0-9]{3}$/', $str)) {
            // every comma is followed by three digits and there are at least two of them
            return str_replace(',', '', $str);
        } else {
            // So this is it. Single comma. We could check if the comma is followed by three digits, but it still could be legitimate and while we want to support unexpected input we do not want to interfere with valid input like "1,234" meant as 1.234
            return str_replace(',', '.', $str);
        }
    }
}

例子:

function formated_test($str) {
    $num = stringToNumber($str);
    printf("% 14s => % 14s\n", $str, $num);
}

formated_test("42");
formated_test("42.1");
formated_test("42,1");
formated_test("42.123");
formated_test("42,123");
formated_test("42.123,42");
formated_test("42,123.42");
formated_test("42.123.456");
formated_test("42,123,456");
formated_test("42.123.456,12");
formated_test("42,123,456.12");

然后输出:

            42 =>             42
          42.1 =>           42.1
          42,1 =>           42.1
        42.123 =>          42123
        42,123 =>         42.123
     42.123,42 =>       42123.42
     42,123.42 =>       42123.42
    42.123.456 =>       42123456
    42,123,456 =>       42123456
 42.123.456,12 =>    42123456.12
 42,123,456.12 =>    42123456.12

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多