-2

I made a code for calculating Woocommmerce discount... Sorry for the stupid question, but I can not find a solution.

"division" outputs the "price_per_bottle" devided by "price_per_ltr". I can not get it to output only 2 decimal places ("12.30" as in €), because now, by default, there are many.

<button id='add' type='button' onclick='ik(this.value);'  value='3'>
30% Discount <span class="kalkc3" id="kalk1"><?php 
$price_per_bottle= $product->get_price();
$price_per_ltr= 0.7; 
$division = $price_per_bottle/ $price_per_ltr; 
echo $division;
?></span></button>

I have found the answer, but not in the mentioned post... But with trial and error:)..

<?php 
$kokstane3 = $product->get_price(); 
$popust3 = 0.7; 
$rezultat3 = $kokstane3 * $popust3;
echo (round($rezultat3,2));// <= HERE
?>
  • 2
    echo round($division, 2); – Stender Nov 16 '18 at 09:29
  • note - `round()` will not work, if you have thousands seperator – Stender Nov 16 '18 at 09:31
  • in which case - you should go with `number_format` like the answer explains – Stender Nov 16 '18 at 09:32
  • I highly recommend not to ever use non-English identifiers when programming. – connexo Nov 16 '18 at 09:36
  • This question appears to be related to PHP, not Javascript/jQuery. Edited the tags. – connexo Nov 16 '18 at 09:37
  • I found the answer, but, not in the mentioned post... But by trial and error:) – Andrej Gjurin Nov 16 '18 at 11:40
  • The solution from the edit is in the linked post: https://stackoverflow.com/a/8338830/466862, https://stackoverflow.com/a/4483553/466862 You should not only look at the accepted answer, but also at the other (upvoted) answers. An accepted answer is not necessarily the only (nor the best) answer. – Mark Rotteveel Nov 17 '18 at 10:07

1 Answers1

2

Try with number_format()

$flaschenpreis = $product->get_price();
$literpreis = 0.7; 
$division = $flaschenpreis / $literpreis; 

$division = number_format((float)$division , 2, '.', '');
Hary
  • 5,690
  • 7
  • 42
  • 79