Ask a Question related to PHP Development, Design and Development.
-
elyob #1
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
-
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... -
PHP Truncate Text
Can anyone please tell me how to truncate dynamic PHP text - is there aan extension available that does this ?? Thanks............ -
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... -
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... -
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... -
Pedro Graca #2
Re: Truncate a decimal pointed figure without rounding ...
elyob wrote:
Something like this?> 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
<?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
-
Eric Veltman #3
Re: Truncate a decimal pointed figure without rounding ...
Hello,
elyob wrote:
After making a function myself that was just far more complex> 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
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
-
elyob #4
Re: Truncate a decimal pointed figure without rounding ...
"Eric Veltman" <eric@[RemoveThis]veltman.nu> wrote in message
news:vqnuqliq258v4f@corp.supernews.com...<snip>> 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...
Couldn't get this to work. In the end I exploded by the "." and substr'd the
[1]
Thanks for help
elyob Guest
-
elyob #5
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...before> 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 numberDidn't seem to want to work for me ... found a different method (as in other>> > 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";
> ?>
reply).
Thanks
elyob Guest
-
Greg #6
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



Reply With Quote

