Truncate a decimal pointed figure without rounding ...

This is a discussion on Truncate a decimal pointed figure without rounding ... within the PHP Language forums, part of the PHP Programming Forums category; How can I get -0.162058 to -0.1 rather than rounding to -0.2? I know I'm being ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-07-2003
elyob
 
Posts: n/a
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


Reply With Quote
  #2 (permalink)  
Old 11-07-2003
Pedro Graca
 
Posts: n/a
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
Reply With Quote
  #3 (permalink)  
Old 11-07-2003
Eric Veltman
 
Posts: n/a
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
Reply With Quote
  #4 (permalink)  
Old 11-08-2003
elyob
 
Posts: n/a
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


Reply With Quote
  #5 (permalink)  
Old 11-08-2003
elyob
 
Posts: n/a
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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:49 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0