471,852 Members | 913 Online

# 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
Jul 17 '05 #1
4 13890
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
Jul 17 '05 #2
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
Jul 17 '05 #3

"Eric Veltman" <eric@[RemoveThis]veltman.nu> wrote in message
news:vq************@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
Jul 17 '05 #4

"Pedro Graca" <he****@hotpop.com> wrote in message
news:bo*************@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
Jul 17 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 21 posts views Thread by Batista, Facundo | last post: by 2 posts views Thread by RC | last post: by 1 post views Thread by | last post: by reply views Thread by | last post: by 2 posts views Thread by Thor W Hammer | last post: by 3 posts views Thread by mlafarlett | last post: by 248 posts views Thread by md | last post: by 1 post views Thread by chinni0719 | last post: by 5 posts views Thread by bodycode | last post: by reply views Thread by NeoPa | last post: by reply views Thread by antdb | last post: by 2 posts views Thread by guiromero | last post: by 9 posts views Thread by CD Tom | last post: by 2 posts views Thread by beacampos | last post: by reply views Thread by isladogs | last post: by reply views Thread by beacampos | last post: by 1 post views Thread by RonaldHiggins22 | last post: by reply views Thread by isladogs | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.