How to do this in php

Discussion in 'PHP' started by lowridertj, Sep 30, 2012.

  1. #1
    What im looking to do is format numbers in this way

    $3.0 T
    $838.3 B
    $838.3 M
    $1,000
    $798

    So that it trims it down to the whole number and decimal for trilling, billion, million etc..

    I know if i played with it long enough I would figure it out, but my head is fried at the moment.


    I appreciate the help thank you..
    TJ
     
    lowridertj, Sep 30, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    If larger than 999999999
    format as number / 1000000000 and add 'T'

    Else If larger than 999999
    ...


    You get the idea. If, select case - there are a number of ways to do it.
     
    Rukbat, Oct 1, 2012 IP
  3. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #3
    You wouldnt be able to show an exact php if than \ else if \ else for applied application would you.
    I really hate to sound so lazy, just my mind is wrapping around it at the moment.. Its likely so easy im just not getting it..
     
    lowridertj, Oct 1, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    
    function autoSize($amount) {
    	if ($amount>1000000000000) return number_format($amount/1000000000000,1).' T';
    	if ($amount>1000000000) return number_format($amount/1000000000,1).' B';
    	if ($amount>1000000) return number_format($amount/1000000,1).' M';
    	return number_format($amount,0);
    }
    
    Code (markup):
     
    deathshadow, Oct 3, 2012 IP
  5. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Rukbat, you pretty much answered your own question!

    One way you can learn and teach yourself is, assuming you know fundamentals of PHP is to write what you require in plain english, and convert it using PHP syntax, trail and error, and debugging, and you'll be teaching and learning yourself :)
     
    Vick.Kumar, Oct 5, 2012 IP
  6. real_person

    real_person Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    @DeathShadow

    i've developmented yours, it's now better.. since asker can easily add formats

     
    real_person, Oct 5, 2012 IP
  7. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #7
    Thanks for the replies everyone..
     
    lowridertj, Oct 8, 2012 IP