Format price with PHP

Discussion in 'PHP' started by Kyriakos, Nov 17, 2011.

  1. #1
    Hi,

    I have an e-shop and i want to format my prices to this format 1350 -> 1.350 or 1548236 -> 1.548.236
    is this possible with PHP?
     
    Last edited: Nov 17, 2011
    Kyriakos, Nov 17, 2011 IP
  2. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #2
    Hello,


    use the below code,


    <?php
    function format_price($num){
    $var=$num;
    $countlent=strlen($var);
    for($i=0;$i < $countlent;$i++){
    if($i==0){
    $streSUb=substr($var,0,1);
    $streSUb123=substr($var,1,$countlent);
    $strStore=$streSUb.".".$streSUb123;
    }
    if($i==3){
    $streSUb=substr($strStore,0,5);
    $streSUb123=substr($strStore,5,$countlent);
    $strStore=$streSUb.".".$streSUb123;
    }

    if($i==6){
    $streSUb=substr($strStore,0,9);
    $streSUb123=substr($strStore,9,$countlent);
    $strStore=$streSUb.".".$streSUb123;
    }


    }
    echo $strStore;
    }


    format_price(12345678);


    ?>
     
    avinash gaud, Nov 17, 2011 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    thanks my friend but i found a very shorter way
    <?php
    setlocale(LC_MONETARY, 'el_GR');
    echo money_format('%.2n', $row['price']);
    ?>
    PHP:
    this is the result "1.350,00€"
     
    Kyriakos, Nov 17, 2011 IP
  4. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #4
    There's another shorter way
    
    <?php
    echo number_format($row['price'], 0, ',', '.');
    ?>
    
    PHP:
     
    xrvel, Nov 17, 2011 IP