2014-10-03 55 views
0

中禁用小数点转​​换时,我提交1,000,000,它显示为1,但如果我提交1000000,它显示为1,000,000。无论提交方式如何,我都希望输出为1,000,000。请帮助!如何在php

这里是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');}}} 
+0

你的问题有点不对。 PHP不会将此值转换为小数分隔值,您的框架可以。在您的主题中查找“theme_thousands_sep”和“theme_dec_point”选项。 – GuyT 2014-10-03 09:17:20

+0

[解析数字但保留负数](http:// stackoverflow。com/questions/19964723/parse-a-number-but-keep-negatives) – Tigger 2014-10-03 09:40:34

+0

GuyT我试图从function.php中禁用它们bt没有任何更改 – 2014-10-03 10:20:09

回答

0

这个问题很容易理解。

doubleval函数将值转换(“转换”)为浮点数。

例如,字符串'1000.55'将被转换为一个浮点数,表示一个近似于1000.55的数字。但字符串“1,000.55”将被转换为1,因为1,000.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_post_meta($post->ID, 'REAL_HOMES_property_price', true)将返回类似“百万”。 我无法测试它,因为我没有所有必需的文件,但试试看。

+0

嗨佩德罗 非常感谢它的魔力,我非常感谢你的时间和帮助,精神!:) – 2014-10-06 06:08:09

0

您处理价格与str_replace()

你是要这个样子之后,如果你想删除逗号之前,您可以删除字符:

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

Victor感谢您的帮助,但str_replace()会导致网站出现错误。有没有其他的方式 – 2014-10-03 10:17:29

+0

我的代码中有一个错字。我现在已经改变了这个 – Victor 2015-04-21 07:06:01

1

看看numfmt_parse。其它更多的信息在这里:http://php.net/manual/en/numberformatter.parse.php

下面是从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"; 
?> 

和预期的结果:

1234567.891 
1234567 
1

这是格式化的数字行:

$formatted_price = number_format($price_digits,$decimals, $decimal_point, $thousands_separator); 

而这些是设置分隔符的行():

$decimal_point = get_option('theme_dec_point'); 
$thousands_separator = get_option('theme_thousands_sep'); 

你可以尝试恢复到与此代码的浮动:

<?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))); 

但它假定选项都没有改变,当然,赢得” t小数点后面的丢失数字。如果第一个假设明显失败,价格将为空。

+0

感谢佩德罗,但代码给这个错误“解析错误:语法错误,意外的$结束我” – 2014-10-03 10:33:48

+0

嗨,威尔逊。最后一行没有经过测试,我添加了一个未初始化的变量,但我相信这个错误与此无关(这意味着有一个额外的或缺少的分隔符:{,或)或“,或'等) 。我编辑答案测试。测试它作为独立文件,并复制函数“formattedprice2float”并使用它。关心。 – 2014-10-03 11:22:16

+0

嗨,佩德罗我再次尝试编辑文件使用您的代码不知道如果我做正确的事情或没有任何办法,我可以寄给你的PHP文件友好 – 2014-10-03 11:42:06