#StackBounty: #magento2 #price #render magento2 display price in 2.5 lac, 3.5 crore,25 lac etc

Bounty: 200

how to display product price in listing page, view page in numbers & letters combination.
for example 2.5 lac,3 crores etc.
in PHP I can get price using following logic.But how to implement in magento2?

<?php

namespace DcwSearchHelper;
use MagentoFrameworkAppHelperContext;
class Price extends MagentoFrameworkAppHelperAbstractHelper {




    public function __construct(
    Context $context
    , array $data = []
    ) {
        parent::__construct($context);
    }

    /**
     * 
     * @return type
     */
    public function getPrice($num) {

    $ext="";//thousand,lac, crore
    $number_of_digits = $this->count_digit($num); //this is call :)
      if($number_of_digits>3)
        {
            if($number_of_digits%2!=0)
              $divider=$this->divider($number_of_digits-1);
            else
               $divider=$this->divider($number_of_digits);
        }
        else
         $divider=1;

        $fraction=$num/$divider;
        $fraction=number_format($fraction,2);
        if($number_of_digits==4 ||$number_of_digits==5)
         $ext="k";
        if($number_of_digits==6 ||$number_of_digits==7)
          $ext="Lac";
        if($number_of_digits==8 ||$number_of_digits==9)
            $ext="Cr";

        return $fraction." ".$ext;
    }


protected function count_digit($number) {
  return strlen($number);
}

protected function divider($number_of_digits) {
    $tens="1";

  if($number_of_digits>8)
    return 10000000;

  while(($number_of_digits-1)>0)
  {
    $tens.="0";
    $number_of_digits--;
  }
  return $tens;
}



}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.