SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again,

    Another quickie....

    How do I convert a number with many decimal places, to just 2 decimal places please (i.e. like with prices)?

    e.g. $total = 1.222222;

    so how would I make $total = 1.22

    Thanks,

    Jason

  2. #2
    SitePoint Member
    Join Date
    May 2000
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jason,

    I had (almost) the same problem. This thread may be able to help you out:

    http://www.sitepointforums.com/showt...threadid=13628


    Andrew.
    http://www.mammoth.com.au/

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hmmmmm not quite,

    Thanks insomniac, but I think this will remove all decimal places!!!

    I'd like to keep 2 of them, e.g. 14.33


    So any other ideas please?


    Regards,

    Jason

  4. #4
    SitePoint Member
    Join Date
    May 2000
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jason,

    I know that if you play around with that code in the other thread, you can get the result your looking for.


    Andrew.
    http://www.mammoth.com.au/

  5. #5
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $var = sprintf( "%.2f", $var );

    Should return $var to 2 decimal places.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perhaps


    $value = 14.8366723786;
    // change to whatever value...

    $value = $value * pow(10, 3);

    $value = floor($value);

    $value = (float) $value/10;
    (float) $modSquad = ($value - floor($value));
    $value = floor($value);
    if ($modSquad > .5){
    $value++;
    }

    $value = $value / (pow(10, 2));

    okay.. I was beaten to it.. and my way is sooooooo complicated but never mind

    <Edited by PeterW on 01-10-2001 at 11:28 AM>

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys!!!

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a function in php for this

    $var = number_format($var, 2);

    where number_format converts the given string to the number of decimal point specified, in this case 2
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •