PHP:十进制格式和mysql

时间:2014-11-02 15:12:31

标签: php mysql format decimal

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

1,1(一点一) - 1.000,90(一千零九十) - 1.000.000,90(百万和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(一千)我把它作为1000保存在DDBB上。 1,2(十进制)我将其保存为1.1。

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

1 个答案:

答案 0 :(得分:0)

你可以轻松地&#34;转换包含逗号和点的字符串,但特别是如果你想支持错位的千位分隔符或允许用户输入三个小数位,就会出现歧义。

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

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