Truncate a decimal pointed figure without rounding ...

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Truncate a decimal pointed figure without rounding ...

    How can I get -0.162058 to -0.1 rather than rounding to -0.2?

    I know I'm being stupid here, but it's really bugging me. The number before
    the decimal point could 0 - 999

    Thanks


    elyob Guest

  2. Similar Questions and Discussions

    1. FlashGateway Pointed to Wrong Server?
      We have Pollster installed on a test server and it's working fine. Test box is running MX7. I ported it over to our live server (currently...
    2. PHP Truncate Text
      Can anyone please tell me how to truncate dynamic PHP text - is there aan extension available that does this ?? Thanks............
    3. Rounding decimal places
      What is the best and most acurate way to round a number to 2 decimal places. It has the be a correctly rounded number not just a mask.. eg: round...
    4. Rounding to 2 decimal places
      I have this placed in my code <%Response.write(Session("MembershipCost"))%> the number it contains is 605 how do i use the round Function to...
    5. Rounding up anything past the decimal point sprintf
      Hi all How can I round a number up if there is any value other than 00 after the decimal point here is what I have: my $count = "5"; my $item...
  3. #2

    Default Re: Truncate a decimal pointed figure without rounding ...

    elyob wrote:
    > How can I get -0.162058 to -0.1 rather than rounding to -0.2?
    >
    > I know I'm being stupid here, but it's really bugging me. The number before
    > the decimal point could 0 - 999
    Something like this?

    <?php
    function number_trim($x) {
    if ($x<0)
    return ceil($x);
    else
    return floor($x);
    }

    $x = -0.162058;
    echo number_trim($x*10), "\n";
    ?>

    --
    ..sig
    Pedro Graca Guest

  4. #3

    Default Re: Truncate a decimal pointed figure without rounding ...

    Hello,

    elyob wrote:
    > How can I get -0.162058 to -0.1 rather than rounding to -0.2?
    > I know I'm being stupid here, but it's really bugging me. The number
    > before the decimal point could 0 - 999
    After making a function myself that was just far more complex
    than needed, I saw in the user contributed notes of the manual
    that someone contributed a suggestion for such function :

    jbeardsl [found_at] gte [d0t] net
    08-Nov-2002 01:15
    I was looking for a truncate function. Not finding one, I wrote my own.
    Since it deals with everything as a number, I imagine it's faster than the
    alternative of using string functions. HTH...

    function truncate ($num, $digits = 0) {

    //provide the real number, and the number of
    //digits right of the decimal you want to keep.

    $shift = pow(10, $digits);
    return ((floor($num * $shift)) / $shift);
    }

    Best regards,

    Eric
    Eric Veltman Guest

  5. #4

    Default Re: Truncate a decimal pointed figure without rounding ...


    "Eric Veltman" <eric@[RemoveThis]veltman.nu> wrote in message
    news:vqnuqliq258v4f@corp.supernews.com...
    > Hello,
    >
    > elyob wrote:
    >
    > > How can I get -0.162058 to -0.1 rather than rounding to -0.2?
    > > I know I'm being stupid here, but it's really bugging me. The number
    > > before the decimal point could 0 - 999
    >
    > After making a function myself that was just far more complex
    > than needed, I saw in the user contributed notes of the manual
    > that someone contributed a suggestion for such function :
    >
    > jbeardsl [found_at] gte [d0t] net
    > 08-Nov-2002 01:15
    > I was looking for a truncate function. Not finding one, I wrote my own.
    > Since it deals with everything as a number, I imagine it's faster than the
    > alternative of using string functions. HTH...
    <snip>

    Couldn't get this to work. In the end I exploded by the "." and substr'd the
    [1]

    Thanks for help


    elyob Guest

  6. #5

    Default Re: Truncate a decimal pointed figure without rounding ...


    "Pedro Graca" <hexkid@hotpop.com> wrote in message
    news:bogtui$1cukm8$1@ID-203069.news.uni-berlin.de...
    > elyob wrote:
    > > How can I get -0.162058 to -0.1 rather than rounding to -0.2?
    > >
    > > I know I'm being stupid here, but it's really bugging me. The number
    before
    > > the decimal point could 0 - 999
    >
    > Something like this?
    >
    > <?php
    > function number_trim($x) {
    > if ($x<0)
    > return ceil($x);
    > else
    > return floor($x);
    > }
    >
    > $x = -0.162058;
    > echo number_trim($x*10), "\n";
    > ?>
    Didn't seem to want to work for me ... found a different method (as in other
    reply).

    Thanks


    elyob Guest

  7. #6

    Default Re: Truncate a decimal pointed figure without rounding ...

    in php, treat the variable as a string, and use substr($var,0,x), where 0 is the start of the string and x is the number of total 'characters' you want for you number. This assumes you know that you will only have 2 (for example) numbers preceding the decimal.
    Greg Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139