2016-11-16 83 views
1

我正在研究Zen Cart中的某些内容,并且我不希望.00显示价格。 我以为我解决了该问题通过做检查数字是否以.00结尾使用php

$price=number_format($special_price,2,'.',''); 

但禅车具有如下功能:添加所需的货币符号的前面或

$currencies->format($price) 

的问题是,这个功能增加了数回.00回到价值!

该函数的代码是

$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(zen_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right']; 

如果我复制该函数,这样我有$ currencies-> FORMAT2($价格)并将其更改为

$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(zen_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['0'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right']; 

则反而会加重货币符号不加小数点后退。当然,当你有49.50这样的价格时,它就是四舍五入到最高达到50

我尝试了

$cur_price=$currencies->format($special_price); 
    $price=str_replace(".00", "", (string)number_format ($cur_price, 2, ".", "")); 

它背后的思想是,我可以先应用货币符号,然后删除小数,如果它们是.00,但这导致价格应该是空白。

我要么需要找到一个方法来检查,如果

$price 

在.00结束,所以我可以有条件地currencies- $>格式()或$ currencies-> FORMAT2(),或者我需要修改原始函数,如果它为.00,则不要将小数位放在适当位置,但在其他时间允许使用。

回答

4

$price = substr($price, -3) == ".00" ? substr($price, 0, -3) : $price; 

工作?

+0

可以结束第一轮数到小数点后两位的东西后,再次运行呢? – atoms

+0

@泰勒塞巴斯蒂安,工作完美。谢谢。 –

1

您可以使用PHP的explode()函数将价格分成两部分(小数前后的部分),然后检查是否它是您想要的。

尝试运行下面的代码,然后改变$curr_price在00

<?php 
$curr_price = '45.99'; 
$price_array = explode('.', $curr_price); 
if ($price_array[1] == '00') 
{ 
    $curr_price = $price_array[0]; 
} 
echo 'The price is ' . $curr_price . "<br>"; 
?> 
相关问题