如何在php中禁用小数点转换

如何在php中禁用小数点转换,php,Php,当我提交1000000时,它显示为1,但如果我提交1000000,它显示为1000000。我希望输出为1000000,无论它是如何提交的。请帮忙 以下是PHP代码: if(!function_exists('get_property_price')){ function get_property_price(){ global $post; $price_digits = doubleval(get_post_meta($post->ID, 'REA

当我提交
1000000
时,它显示为
1
,但如果我提交
1000000
,它显示为
1000000
。我希望输出为
1000000
,无论它是如何提交的。请帮忙

以下是PHP代码:

if(!function_exists('get_property_price')){
    function get_property_price(){
        global $post;
        $price_digits = doubleval(get_post_meta($post->ID, 'REAL_HOMES_property_price', true));
        if($price_digits){
            $currency = get_theme_currency();
            $price_post_fix = get_post_meta($post->ID, 'REAL_HOMES_property_price_postfix', true);
            $decimals = intval(get_option( 'theme_decimals'));
            $decimal_point = get_option( 'theme_dec_point' );
            $thousands_separator = get_option( 'theme_thousands_sep' );
            $currency_position = get_option( 'theme_currency_position' );
            $formatted_price = number_format($price_digits,$decimals, $decimal_point, $thousands_separator);
            if($currency_position == 'after'){
                return $formatted_price . $currency. ' ' . $price_post_fix;
            }else{
                return $currency . $formatted_price . ' ' . $price_post_fix;
            }
        }else{
            return __('NA','framework');
        }
    }
}

if(!function_exists('property_price')){
    function property_price(){
        echo get_property_price();
    }
}

if(!function_exists('get_custom_price')){
    function get_custom_price($amount){
        $amount = doubleval($amount);
        if($amount){
            $currency = get_theme_currency();
            $decimals = intval(get_option( 'theme_decimals'));
            $decimal_point = get_option( 'theme_dec_point' );
            $thousands_separator = get_option( 'theme_thousands_sep' );
            $currency_position = get_option( 'theme_currency_position' );
            $formatted_price = number_format($amount,$decimals, $decimal_point, $thousands_separator);
            if($currency_position == 'after'){
                return $formatted_price . $currency;
            }else{
                return $currency . $formatted_price;
            }
        }else{
            return __('NA','framework');}}}

使用
str\u replace()

如果要删除逗号,则以后将显示如下:

$price_digits = doubleval(str_replace(',', '', get_post_meta($post->ID, 'REAL_HOMES_property_price', true));

查看
numfmt\u parse
。更多信息请点击此处:

以下是PHP文档中的示例:

<?php
$fmt = numfmt_create( 'de_DE', NumberFormatter::DECIMAL );
$num = "1.234.567,891";
echo numfmt_parse($fmt, $num)."\n";
echo numfmt_parse($fmt, $num, NumberFormatter::TYPE_INT32)."\n";
?>

这是设置数字格式的行:

$formatted_price = number_format($price_digits,$decimals, $decimal_point, $thousands_separator);
这些是设置分隔符的行():

您可以尝试使用以下代码还原为浮点:

<?php
function get_option($propertyName) {
    static $properties = array('theme_dec_point' => '.',
                               'theme_thousands_sep' => ',');
    return $properties[$propertyName];
}

function formattedprice2float($formatted_price) {
    $decimal_point = get_option('theme_dec_point');
    $thousands_separator = get_option('theme_thousands_sep');

    $price_string = str_replace(array($decimal_point, $thousands_separator),
                                array('.', ''),
                                $formatted_price);
    return is_numeric($price_string) ? (float)$price_string : null;
}

// test
var_dump( formattedprice2float(number_format(100000, 2)) );

这个问题很容易理解

doubleval函数将值转换为浮点数

例如,字符串“1000.55”将转换为一个浮点数,表示一个近似于1000.55的数字。但是字符串“1000.55”将转换为1,因为1000.55不是有效的浮点表示形式。在PHP浮点表示法中不存在逗号,因此字符串在转换为浮点之前会被截断

我相信我实现的功能可以像这样解决您的问题:

function formattedprice2float($formatted_price) {
    $decimal_point       = get_option('theme_dec_point');
    $thousands_separator = get_option('theme_thousands_sep');

    $price_string = str_replace(array($decimal_point, $thousands_separator),
                                array('.', ''),
                                $formatted_price);
    return is_numeric($price_string) ? (float)$price_string : null;
}

if(!function_exists('get_property_price')){
    function get_property_price(){
        global $post;
        $price_digits = formattedprice2float(get_post_meta($post->ID, 'REAL_HOMES_property_price', true));
        if($price_digits){
            $currency = get_theme_currency();
            $price_post_fix = get_post_meta($post->ID, 'REAL_HOMES_property_price_postfix', true);
            $decimals = intval(get_option( 'theme_decimals'));
            $decimal_point = get_option( 'theme_dec_point' );
            $thousands_separator = get_option( 'theme_thousands_sep' );
            $currency_position = get_option( 'theme_currency_position' );
            $formatted_price = number_format($price_digits,$decimals, $decimal_point, $thousands_separator);

            if($currency_position == 'after'){
                return $formatted_price . $currency. ' ' . $price_post_fix;
            }else{
                return $currency . $formatted_price . ' ' . $price_post_fix;
            }
        }else{
            return __('NA','framework');
        }
    }
}
我假设
get\u post\u meta($post->ID,'REAL\u HOMES\u property\u price',true)
返回类似于'1000000'的值。
我无法测试它,因为我没有所有必需的文件,但请尝试一下。

您的问题有点错误。PHP不会将这些值转换为小数分隔的值,您的框架会这样做。在您的主题中,查找选项
theme\u-sep
theme\u-dec\u-point
。可能存在重复的GuyT,我已尝试从函数中禁用它们。没有任何changes@WilsonTish我猜它们是在数据库中声明的。“你不能只是禁用它们。”威尔森蒂什经过进一步调查后,我想你是在WordPress的
Real Homes
团队工作吧?我想您可以在以管理员身份登录时更改此设置。Victor感谢您的帮助,但是str_replace()导致网站出现错误。还有其他方法吗?我的代码有错。我现在已经更改了,谢谢Pedro,但是代码给出了这个错误“Parse error:syntax error,unexpected$end I”嗨,Wilson。最后一行没有测试,我添加了一个未初始化的变量,但我相信错误与此无关(这意味着有一个额外的或缺少的分隔符:{,or),or',等等)。我用测试编辑了答案。将其作为独立文件进行测试,并复制函数“formattedprice2float”“并使用它。问候。嗨,佩德罗。我再次尝试使用你的代码编辑该文件。我不确定我是否做了正确的事情。我有没有办法向你发送php文件?我不知道如何在这里发送私人消息(如果该功能存在)。你可以通过facebook向我发送你的电子邮件地址(如果你有帐户)我会回答的。嗨,佩德罗。非常感谢它的神奇作用。我真的非常感谢你的时间和帮助,继续保持同样的精神!)
<?php
function get_option($propertyName) {
    static $properties = array('theme_dec_point' => '.',
                               'theme_thousands_sep' => ',');
    return $properties[$propertyName];
}

function formattedprice2float($formatted_price) {
    $decimal_point = get_option('theme_dec_point');
    $thousands_separator = get_option('theme_thousands_sep');

    $price_string = str_replace(array($decimal_point, $thousands_separator),
                                array('.', ''),
                                $formatted_price);
    return is_numeric($price_string) ? (float)$price_string : null;
}

// test
var_dump( formattedprice2float(number_format(100000, 2)) );
function formattedprice2float($formatted_price) {
    $decimal_point       = get_option('theme_dec_point');
    $thousands_separator = get_option('theme_thousands_sep');

    $price_string = str_replace(array($decimal_point, $thousands_separator),
                                array('.', ''),
                                $formatted_price);
    return is_numeric($price_string) ? (float)$price_string : null;
}

if(!function_exists('get_property_price')){
    function get_property_price(){
        global $post;
        $price_digits = formattedprice2float(get_post_meta($post->ID, 'REAL_HOMES_property_price', true));
        if($price_digits){
            $currency = get_theme_currency();
            $price_post_fix = get_post_meta($post->ID, 'REAL_HOMES_property_price_postfix', true);
            $decimals = intval(get_option( 'theme_decimals'));
            $decimal_point = get_option( 'theme_dec_point' );
            $thousands_separator = get_option( 'theme_thousands_sep' );
            $currency_position = get_option( 'theme_currency_position' );
            $formatted_price = number_format($price_digits,$decimals, $decimal_point, $thousands_separator);

            if($currency_position == 'after'){
                return $formatted_price . $currency. ' ' . $price_post_fix;
            }else{
                return $currency . $formatted_price . ' ' . $price_post_fix;
            }
        }else{
            return __('NA','framework');
        }
    }
}