Floating point restriction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sake
    New Member
    • Jan 2007
    • 46

    Floating point restriction

    Hey Everyone,
    Just wondering how I would restrict the amount of digits displayed when printing decimal floating point numbers. Not so it wouldnt calculate that many digits, just so it wouldnt display all of them.Hope that makes sense...

    Thanks in advance,
    Sake
  • Amzul
    New Member
    • Oct 2007
    • 130

    #2
    you can try
    round()
    or if it just for display do something like

    [CODE=php]$floatNumber = 10.0600000009;
    echo substr($floatNu mber,0,-7); // show 10.060[/CODE]

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Yea, round() would do the trick.

      Try something like:
      [code=php]
      $myNumber = 123.456789;
      echo round($myNumber , 3);
      # Which should echo: 123.457
      [/code]

      Or if you want to handle it as a string, try looking into the explode, substr and implode functions.

      Comment

      • sake
        New Member
        • Jan 2007
        • 46

        #4
        Thanks alot for the help, the round() function works great. :-)

        Comment

        Working...