Posted By

arjunparsi on 09/02/11


Tagged

php format currency Indian Grouping


Versions (?)

PHP function for Digits Grouping in indian format


 / Published in: PHP
 

  1. <?php
  2. function convertit($num){
  3. $pos = strpos($num, ".");
  4. if ($pos !== false) {
  5. $ps = substr($num,-2);
  6. $num = substr($num,0,-3);
  7. } else {
  8. $ps = "00";
  9. }
  10. if(strlen($num)>3){
  11. $lastthree = substr($num, strlen($num)-3, strlen($num));
  12. $restunits = substr($num, 0, strlen($num)-3);
  13. $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits;
  14.  
  15. $expunit = str_split($restunits, 2);
  16. for($i=0; $i<sizeof($expunit); $i++){
  17. $explrestunits .= (int)$expunit[$i].",";
  18. }
  19.  
  20. $thecash = $explrestunits.$lastthree;
  21. } else {
  22. $thecash = $convertnum;
  23. }
  24.  
  25. return $thecash.".".$ps;
  26. }
  27. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: keshavkshirsagar on October 10, 2012

Hi

Wrong output 11,0,000.00 for 1100000 Which should be 11,00,000.00 modification required @ line number 17 as

   $explrestunits .= $expunit[$i].","; 

And line number 25 as

   return ltrim($thecash.".".$ps);
Posted By: keshavkshirsagar on October 10, 2012

Or Line number 25 will be

return ltrim($thecash.".".$ps,"0");

You need to login to post a comment.