I have a variable which value is $79.00 I need to get the 70% of this value which is *0.7 but when I try it returns 0. I have tried str_replace("$", "", $price); but still it doesn't work. any advice?
<?php
$price2 = woocommerce_price($product->min_variation_price); // string(6) "$79.00"
$floated = (float) str_replace('/&.*?;/', '', $price2); // float(79)
$seventyPercent = $floated * 0.7; // float(55.3)
$reFormatted = '$' . number_format($seventyPercent, 2); // string(6) "$55.30"
?>
答案 0 :(得分:0)
由于您尚未共享您正在使用的代码,因此很难知道它为何无效。 As you can see,这并不困难:
$price = '$79.00'; // string(6) "$79.00"
$floated = (float) str_replace('$', '', $price); // float(79)
$seventyPercent = $floated * 0.7; // float(55.3)
$reFormatted = '$' . number_format($seventyPercent, 2); // string(6) "$55.30"