-1

我无法让我的百分比计数器功能使用英文货币格式

function calc_proc($price, $savings) {
    $old_price = $price + $savings;

    return (number_format($savings / ($old_price/100)));
}

因为逗号我得到了不好的价值

4

5 回答 5

2

英文货币格式是这样的吗:253,17?

如果是,那么只需执行以下操作:

str_replace(',', '.', $value); 

那你就安全了。

于 2012-09-14T09:22:43.523 回答
1

首先,你不必在财务计算中使用字符串,但如果你仍然必须使用,你应该用点替换逗号。例如,

$a = '1,5';
$b = '2,1';
echo $a + $b; //result 3

// you can avoid this, by replacing comma:
$a = str_replace(',', '.', $a);
//same for $b

另一种解决方案可能是设置语言环境,但在那之后你会遇到点问题。

在您的函数中,它将如下所示:

function calc_proc($price, $savings) {
    $price = str_replace(',', '.', $price);
    $savings = str_replace(',', '.', $savings);
    $old_price = (float)$price + (float)$savings;

    return (number_format($savings / ($old_price/100)));
}
于 2012-09-14T09:21:32.870 回答
0
function percent($price, $savings) {
  $count1 = str_replace(',', '.', $price)/ $savings;
  $count2 = $count1 * 100;
  $count = number_format($count2, 0);
  return $count;
}
于 2012-09-14T09:28:17.097 回答
0

setlocale(constant, location)在脚本的开头尝试。在你的情况下:

setlocale(LC_ALL,"En-Us");
于 2012-09-14T09:22:56.523 回答
0

不带参数的 PHP number_format() 函数返回带逗号的英文格式。您应该改用:

return round($savings / ($old_price/100), 0);

并确保 $old_price 不能为 0

于 2012-09-14T09:24:20.073 回答