SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    precision in floats and doubles

    How can I limit the decimal places to the right of the decimal point in float and double variables to only two places?

    I've tried using this:
    PHP Code:
    ini_set('precision'"2"); 
    but that limits the whole number to 2 decimal places.

    I want the results of divisions to be like this: 0.00, 0.53, 5.74, 599.95, 111102.06, and so on... (just examples).

    Thanks!

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    number_format($num, 2);

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    number_format($num, 2);
    That solves my problem, thanks!

  4. #4
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can also use the round() function

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @taliesinnz: that's also useful thanks!

    But now I want to store the floating point numbers in a MySQL table, is the following column type vaild?

    Code:
    DOUBLE(10,2)
    Also does the 10 refer to the places to the left of the decimal point or to both left and right places?

  6. #6
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chem3 View Post
    Code:
    DOUBLE(10,2)
    Also does the 10 refer to the places to the left of the decimal point or to both left and right places?
    The first is TOTAL amount of digits, right is digits after the decimal..

    If you just insert your float( without rounding) mysql will round it automatically for you

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by taliesinnz View Post
    The first is TOTAL amount of digits, right is digits after the decimal..

    If you just insert your float( without rounding) mysql will round it automatically for you
    In this case MySQL will auto-round to the nearest hundredths not to the nearest ones, right?

  8. #8
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chem3 View Post
    In this case MySQL will auto-round to the nearest hundredths not to the nearest ones, right?
    I would think so,l cant get on MySQL's web site right now to confirm!


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
  •