PHP decimal point help

Discussion in 'PHP' started by primster7, May 23, 2015.

  1. #1
    I have this code that doesn't display the 2nd number after the decimal point. This number is used for money.

    Example:
    $125.0 what I need is $125.00
    $125.4 what I need is $125.40

    Here is the line of code that I need to change:
    $item_price = $item->sellingStatus->currentPrice;

    Any help would be appreciated.
     
    primster7, May 23, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    $item_price = number_format($item->sellingStatus->currentPrice, 2);
    PHP:
     
    Anveto, May 23, 2015 IP
  3. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks Markus, but the code still shows just 1 zero after the decimal point.
     
    primster7, May 23, 2015 IP
  4. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #4
    Ah sorry, the $ sign breaks it i guess

    
    $item_price = money_format('$%i',str_replace("$","",$item->sellingStatus->currentPrice));
    
    PHP:
     
    Anveto, May 23, 2015 IP
  5. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Markus,

    That kind of works... I now get two $ symbols and there is no comma when the dollar amount gets into the thousands. However, there is the .00 now instead of 100.0 when it shows $100.00
    I really appreciate you working on this. If you can find a solution that would be great.
     
    primster7, May 23, 2015 IP
  6. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #6
    Anveto, May 23, 2015 IP
  7. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #7
    This works when I take out the $ symbol. It just doesn't add the comma for some reason.
    $item_price = money_format('%i',str_replace("$","",$item->sellingStatus->currentPrice));
     
    primster7, May 23, 2015 IP
  8. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #8
    Try doing setlocale(LC_MONETARY, 'en_US'); and also change the "$" to '$' and it should work with the $ symbol included or just change my str_replace with flatval()
     
    Anveto, May 23, 2015 IP
  9. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #9
    That worked good except it now has the big USD between the dollar symbol and the number.
    Example:
    $USD 1,349.00
     
    primster7, May 23, 2015 IP
  10. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #10
    Last try then :)

    
    setlocale(LC_MONETARY, 'en_US');
    $item_price = money_format('%(#1n',str_replace('$','',"$1220939938430.00"));
    echo $item_price;
    
    PHP:
     
    Anveto, May 23, 2015 IP
  11. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #11
    Remove the dollar sign before doing number format:

    
    <?php
    $item_price = '$' . number_format(str_replace('$', '', $item->sellingStatus->currentPrice), 2);
    
    
    PHP:
     
    ThePHPMaster, May 24, 2015 IP
  12. primster7

    primster7 Well-Known Member

    Messages:
    801
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #12
    This one worked perfect thanks a lot.
     
    primster7, May 24, 2015 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #13
    My question would be why the **** is there a $ in the stored value in the first place?
     
    deathshadow, May 24, 2015 IP
  14. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #14
    ThePHPMaster, May 24, 2015 IP