Dev
Dev

Reputation: 479

How to set data type for money in mysql

I am new to php & mysql..& i want to store money in database like 45,000 or 21,000 ....

Right now I am using datatype decimal(50,2) , But it shows only 45 when i put 45,000...

If i put 45000 ...then it results ok.......But I want to put 45,000 like this...

what datatype should i use...so it display currectly??

If i use varchar(50), then i cant sort that data...thats the problem....

Please give suggestion..

Thanks

Upvotes: 0

Views: 2625

Answers (2)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use this

 <?php
 function money($value) {
     return "$".number_format($value);
 }

 $money = 2000.00;

 echo money($money);

 // returns $2,000.00

 ?>

use below function

$amount = '100000';
$amount = moneyFormatIndia( $amount );
echo $amount;

function moneyFormatIndia($num){
$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.
}

// output 1,00,000

http://codepad.viper-7.com/ptxvrU

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77966

Just use use decimal(10,2) field type and use money_format() in PHP

Upvotes: 1

Related Questions