How do you put a discount on the goods in the right form?



  • There's a grocery store for wordpress+woo trade. I'll just count the merchandise discount, but something doesn't work out--- get the format out. 1.35a 1.350 rub... What's wrong?

    <p class="economy">
      <?php 
       function extract_unit($string, $start, $end)
       {
          $pos = stripos($string, $start);
          $str = substr($string, $pos);
          $str_two = substr($str, strlen($start));
          $second_pos = stripos($str_two, $end);
          $str_three = substr($str_two, 0, $second_pos);
          $unit = trim($str_three); // remove whitespaces
          return $unit;
        }
       global $product; 
       $price=$product->get_price_html(); 
       $saleprice = extract_unit($price,'<ins><span class="amount">','<ruble title="руб.">a</ruble></span></ins>');
       $regprice = extract_unit($price,'<del><span class="amount">','<ruble title="руб.">a</ruble></span></del>');     
       $economy= $regprice - $saleprice;
       echo $economy." <ruble title='руб.'>a</ruble>"; ?>
    </p>
    

    echo $product->get_price_html();

    shall be so issued:

    <p class="price">
    <del><span class="amount">21.150&nbsp;<ruble title="руб.">a</ruble>
    </span></del>
    <ins><span class="amount">19.800&nbsp;<ruble title="руб.">a</ruble>
    </span></ins>
    </p>



  • Use sprintf:

    For echo $economy." <ruble title='руб.'>a</ruble>";

    Try it. echo sprintf("%.3f"." <ruble title='руб.'>a</ruble>", $economy);

    $number = 1234.56; 
    

    // английский формат (по умолчанию)
    $english_format_number = number_format($number);
    // 1,234

    // французский формат
    $nombre_format_francais = number_format($number, 2, ',', ' ');
    // 1 234,56

    $number = 1234.5678;
    // английский формат без разделителей групп
    $english_format_number = number_format($number, 2, '.', '');
    // 1234.57

    Source: http://php.net/manual/ru/function.number-format.php




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2