float reduced

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thierry

    float reduced

    Hello,

    I'd like to write a function who return a float with only two digits
    after the point.

    For instance : 12.34567 -> 12.34

    Any help would be welcome

    Tkx, Titi.
  • Janwillem Borleffs

    #2
    Re: float reduced

    Thierry wrote:[color=blue]
    > I'd like to write a function who return a float with only two digits
    > after the point.
    >
    > For instance : 12.34567 -> 12.34
    >
    > Any help would be welcome
    >[/color]




    JW



    Comment

    • Thierry

      #3
      Re: float reduced

      Janwillem Borleffs a écrit :[color=blue]
      > Thierry wrote:
      >[color=green]
      >>I'd like to write a function who return a float with only two digits
      >>after the point.
      >>
      >>For instance : 12.34567 -> 12.34
      >>
      >>Any help would be welcome
      >>[/color]
      >
      >
      > http://www.php.net/round[/color]

      Exactly what i needed,
      Thx, Titi

      Comment

      • Hero Wanders

        #4
        Re: float reduced

        Hello!

        [color=blue][color=green]
        >>I'd like to write a function who return a float with only two digits
        >>after the point.
        >>
        >>For instance : 12.34567 -> 12.34
        >>[/color]
        >
        > http://www.php.net/round[/color]

        That *rounds* the value instead of returning the float with only the
        last to digits of it (ok actually Thierry asked for a function which
        returns *a* float (function give($float) { return 3.14; } would do it)).

        I think

        function cutFloat($float )
        {
        return (float) floor($float*10 0)/100;
        }

        is what Thierry really searches.

        Greetings,
        Hero Wanders

        Comment

        • Janwillem Borleffs

          #5
          Re: float reduced

          Hero Wanders wrote:[color=blue]
          > That *rounds* the value instead of returning the float with only the
          > last to digits of it (ok actually Thierry asked for a function which
          > returns *a* float (function give($float) { return 3.14; } would do
          > it)).[/color]

          When you would have bothered to look at the manual page, you would have
          noticed that the round function accepts an optional second argument to
          indicate the precision.


          JW



          Comment

          • Hero Wanders

            #6
            Re: float reduced

            Hello!
            [color=blue][color=green]
            >>That *rounds* the value instead of returning the float with only the
            >>last to digits of it (ok actually Thierry asked for a function which
            >>returns *a* float (function give($float) { return 3.14; } would do
            >>it)).[/color]
            >
            > When you would have bothered to look at the manual page, you would have
            > noticed that the round function accepts an optional second argument to
            > indicate the precision.[/color]

            I knew this argument but the function still *rounds*.
            That means:

            3.145 --> 3.15
            3.144 --> 3.14

            My function gives:

            3.145 --> 3.14
            3.144 --> 3.14


            Greetings,
            Hero

            Comment

            Working...