SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Decimal Places

  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    How do ensure a data is not longer than 0.1 decimal places? thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    $var = 3.222222;
    $var = number_format($var, 1);
    echo($var);      // Prints "3.2"
    That should do the trick.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm
    This is my code:

    $average_rate = round($average_rate, 2);

    Is it the same?
    Its to two decimal places
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not exactly the same. round() rounds the number off to the number of decimal places you specify, while number_format just cuts it down to the number of places you specify.

    Here's an example:

    Code:
    $num = "3.456";
    $num = round($num, 2);
    echo($num);      // Prints "3.46"
    
    /* -------------------------------- */
    
    $num2 = "3.456";
    $num2 = number_format($num2, 2);
    echo($num2);      // Prints "3.45"
    See the difference?

  5. #5
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh..icic..okie
    I'll use round.
    ahahz.its for statiscal uses
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


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
  •