1

I am trying to convert number to indian money format LIke

100000 = 1,00,000

I have use

$explrestunits = "" ;
        if(strlen($num)>3){
            $lastthree = substr($num, strlen($num)-3, strlen($num));
            $restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
            $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
            $expunit = str_split($restunits, 2);
            for($i=0; $i<sizeof($expunit); $i++){
                // creates each of the 2's group and adds a comma to the end
                if($i==0)
                {
                    $explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
                }else{
                    $explrestunits .= $expunit[$i].",";
                }
            }
            $thecash = $explrestunits.$lastthree;
        } else {
            $thecash = $num;
        }
        return $thecash; // writes the final format where $currency is the currency symbol.

But when i Use Minus sign (-) with 10000 (Five Digit Number) It returns Output 0,10,000

It should be -10,000 Have you any idea..

Gopal Joshi
  • 2,350
  • 22
  • 49
  • `money_format()` depends on locale. Set locale before calling the function. You don't need to write your own `money_format()` function – Raptor Oct 28 '13 at 07:21
  • @User016,Its not any duplication i want to find bug in my function, not another solution. So,read question first and then go for any comment.. – Gopal Joshi Oct 28 '13 at 07:53
  • 1
    Before posting the question you must serach for existing questions.Check if similar one exists. @Sameer – 웃웃웃웃웃 Oct 28 '13 at 08:03
  • Yaa man know that very much but i post question about bug in my function, **NOT FOR ANOTHER FUNCTION **.@USER016 – Gopal Joshi Oct 28 '13 at 08:08

1 Answers1

3

Try something like this

<?php
$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
$amount=explode('.',$amount); //Comment this if you want amount value to be 1,00,000.00
echo $amount[0]; //Comment this if you want amount value to be 1,00,000.00

OUTPUT:

1,00,000

EDIT :

But when i Use Minus sign (-) with 10000 (Five Digit Number) It returns Output 0,10,000

It should be -10,000 Have you any idea..

The above code gives you -10,000 if you set $amount = '-10000';

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Can you tell how to get the two digits after decimal point in this? – zegulas Jul 07 '17 at 06:57
  • Unfortunately, this will fail on windows platform... `money_format()` will return `undefined`. **EDIT:** I did notice that OP already was using this function so, it's not a problem for him, but it's worth mentioning in answer for future visitors of this post – Fr0zenFyr Oct 25 '17 at 13:42