1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is there a way to split a dollar amount?

Discussion in 'PHP' started by Greenmethod, Dec 5, 2007.

  1. #1
    I need to split a dollar amount into dollars and cents. Any ideas?

    Thanks!
     
    Greenmethod, Dec 5, 2007 IP
  2. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    My suggestion :
    
    $amount   = 4.95; //or whatever you want
    $dollars  = floor($amount);
    $cents    = 100*($amount - $dollars);
    echo "\$$amount or $dollars dollars and $cents cents";
    
    PHP:
    outputs
    $4.95 or 4 dollars and 95 cents
     
    selling vcc, Dec 5, 2007 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    
    $amount   = 4.95; //or whatever you want
    list($dollars,$cents) = explode('.',$amount);
    echo "\$$amount or $dollars dollars and $cents cents";
    
    PHP:
    Peace,
     
    Barti1987, Dec 5, 2007 IP
    tarponkeith and selling vcc like this.
  4. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah!... that's better
     
    selling vcc, Dec 5, 2007 IP
  5. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #5
    
    $amount   = 4.95; //or whatever you want
    $dollars  = substr($amount,0,strpos($amount,'.'));
    $cents    = substr($amount,strpos($amount,'.')+1);
    echo "\$$amount or $dollars dollars and $cents cents";
    
    PHP:
     
    coderbari, Dec 6, 2007 IP
    selling vcc likes this.