Need unrounded precision

Need unrounded precision

am 12.10.2009 02:35:07 von Andre Dubuc

Hi,

I need to extract the first digit after the decimal point from a number such
as 28.56018, which should be '5'.

I've tried a few methods to accomplish this. If I use 'ini_set' I would need
to know the number of digits before the decimal (which, unfortunately, I
would not have access to).

Then I've tried:


$elapsed = 28.56018;

$digit = round($elapsed, 1); // rounds result is '6'
$digit = number_format($elapsed, 1); // still rounds result to '6'

?>

What I need is only the first digit after the decimal -- all the rest could
be 'chopped' or discarded but without rounding the first digit after the
decimal point.

Is there any way of doing this?

I'm stumped.

Tia,
Andre

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 12.10.2009 02:50:11 von Eddie Drapkin

On Fri, Jan 1, 2010 at 9:20 PM, Andre Dubuc wrote:
> Hi,
>
> I need to extract the first digit after the decimal point from a number s=
uch
> as 28.56018, which should be '5'.
>
> I've tried a few methods to accomplish this. If I use 'ini_set' I would n=
eed
> to know the number of digits before the decimal (which, unfortunately, I
> would not have access to).
>
> Then I've tried:
>
> >
>        $elapsed =3D 28.56018;
>
>        $digit =3D round($elapsed, 1); // rounds resul=
t is '6'
>        $digit =3D number_format($elapsed, 1); // stil=
l rounds result to '6'
>
> ?>
>
> What I need is only the first digit after the decimal -- all the rest cou=
ld
> be 'chopped' or discarded but without rounding the first digit after the
> decimal point.
>
> Is there any way of doing this?
>
> I'm stumped.
>
> Tia,
> Andre
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Try sprintf with a format of %0.1f.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision [RESOLVED]

am 12.10.2009 03:06:20 von Andre Dubuc

Thanks Eddie,

Actually while waiting for some re[lies, I resolved the problem.

I realized that '$elapsed' will always have only two digits before the deci=
mal=20
point. Thus I was able to use 'number_format($elapsed, 2);' and to give me=
=20
the desired result.

Thanks for the quick reply!
Andre



On October 11, 2009 08:50:11 pm Eddie Drapkin wrote:
> On Fri, Jan 1, 2010 at 9:20 PM, Andre Dubuc wrote:
> > Hi,
> >
> > I need to extract the first digit after the decimal point from a number
> > such as 28.56018, which should be '5'.
> >
> > I've tried a few methods to accomplish this. If I use 'ini_set' I would
> > need to know the number of digits before the decimal (which,
> > unfortunately, I would not have access to).
> >
> > Then I've tried:
> >
> > > >
> >        $elapsed =3D 28.56018;
> >
> >        $digit =3D round($elapsed, 1); // rounds res=
ult is '6'
> >        $digit =3D number_format($elapsed, 1); // st=
ill rounds result to '6'
> >
> > ?>
> >
> > What I need is only the first digit after the decimal -- all the rest
> > could be 'chopped' or discarded but without rounding the first digit
> > after the decimal point.
> >
> > Is there any way of doing this?
> >
> > I'm stumped.
> >
> > Tia,
> > Andre
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
> Try sprintf with a format of %0.1f.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 12.10.2009 03:14:56 von List Manager

Andre Dubuc wrote:
> Hi,
>
> I need to extract the first digit after the decimal point from a number such
> as 28.56018, which should be '5'.
>
> I've tried a few methods to accomplish this. If I use 'ini_set' I would need
> to know the number of digits before the decimal (which, unfortunately, I
> would not have access to).
>
> Then I've tried:
>
> >
> $elapsed = 28.56018;
>
> $digit = round($elapsed, 1); // rounds result is '6'
> $digit = number_format($elapsed, 1); // still rounds result to '6'
>
> ?>
>
> What I need is only the first digit after the decimal -- all the rest could
> be 'chopped' or discarded but without rounding the first digit after the
> decimal point.
>
> Is there any way of doing this?
>
> I'm stumped.
>
> Tia,
> Andre
>

FYI: did you know that your computer thinks it is in the future?

You need to check your date settings...

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 12.10.2009 03:16:02 von Andre Dubuc

Yup! I had noticed that I had forgot to reset to today's date after working on
the code, just after I sent the messages.

Thanks.
Andre



On October 11, 2009 09:14:56 pm Jim Lucas wrote:
> Andre Dubuc wrote:
> > Hi,
> >
> > I need to extract the first digit after the decimal point from a number
> > such as 28.56018, which should be '5'.
> >
> > I've tried a few methods to accomplish this. If I use 'ini_set' I would
> > need to know the number of digits before the decimal (which,
> > unfortunately, I would not have access to).
> >
> > Then I've tried:
> >
> > > >
> > $elapsed = 28.56018;
> >
> > $digit = round($elapsed, 1); // rounds result is '6'
> > $digit = number_format($elapsed, 1); // still rounds result to '6'
> >
> > ?>
> >
> > What I need is only the first digit after the decimal -- all the rest
> > could be 'chopped' or discarded but without rounding the first digit
> > after the decimal point.
> >
> > Is there any way of doing this?
> >
> > I'm stumped.
> >
> > Tia,
> > Andre
>
> FYI: did you know that your computer thinks it is in the future?
>
> You need to check your date settings...
>
> --
> Jim Lucas
>
> "Some men are born to greatness, some achieve greatness,
> and some have greatness thrust upon them."
>
> Twelfth Night, Act II, Scene V
> by William Shakespeare



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Need unrounded precision

am 12.10.2009 08:37:21 von Arno Kuhl

-----Original Message-----
From: Andre Dubuc [mailto:aajdubuc@webhart.net]
Sent: 02 January 2010 03:20 AM
To: php-general@lists.php.net
Subject: [PHP] Need unrounded precision

Hi,

I need to extract the first digit after the decimal point from a number such
as 28.56018, which should be '5'.

I've tried a few methods to accomplish this. If I use 'ini_set' I would need
to know the number of digits before the decimal (which, unfortunately, I
would not have access to).

Then I've tried:


$elapsed = 28.56018;

$digit = round($elapsed, 1); // rounds result is '6'
$digit = number_format($elapsed, 1); // still rounds result to '6'

?>

What I need is only the first digit after the decimal -- all the rest could
be 'chopped' or discarded but without rounding the first digit after the
decimal point.

Is there any way of doing this?

I'm stumped.

Tia,
Andre

--

One way that should work regardless the number of digits before/after the
decimal is:
- convert to string (sprintf or typecast)
- strpos the decimal
- grab the char from the next position

Cheers
Arno


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Need unrounded precision

am 12.10.2009 09:03:10 von chetan rane

May be this will work
$elapsed =3D 28.56018;
$elapsed_rel =3D (int) 28.56018;
$elapsed_deci =3D $elapsed - $elapsed_rel;
$deci =3D ((int) ($elapsed_deci * 10))/10;
$final =3D $elapsed_rel + $deci;


With regards,

Chetan Dattaram Rane | Software Engineer | Persistent Systems
chetan_rane@persistent.co.in=A0 | Cell: +91 9766646714 | Tel: +91 (0832) 30=
79228
Innovation in software product design, development and delivery- www.persist=
entsys.com


-----Original Message-----
From: Arno Kuhl [mailto:akuhl@telkomsa.net]
Sent: Monday, October 12, 2009 12:07 PM
To: 'Andre Dubuc'; php-general@lists.php.net
Subject: RE: [PHP] Need unrounded precision

-----Original Message-----
From: Andre Dubuc [mailto:aajdubuc@webhart.net]
Sent: 02 January 2010 03:20 AM
To: php-general@lists.php.net
Subject: [PHP] Need unrounded precision

Hi,

I need to extract the first digit after the decimal point from a number such
as 28.56018, which should be '5'.

I've tried a few methods to accomplish this. If I use 'ini_set' I would need
to know the number of digits before the decimal (which, unfortunately, I
would not have access to).

Then I've tried:


$elapsed =3D 28.56018;

$digit =3D round($elapsed, 1); // rounds result is '6'
$digit =3D number_format($elapsed, 1); // still rounds result to '6'

?>

What I need is only the first digit after the decimal -- all the rest could
be 'chopped' or discarded but without rounding the first digit after the
decimal point.

Is there any way of doing this?

I'm stumped.

Tia,
Andre

--

One way that should work regardless the number of digits before/after the
decimal is:
- convert to string (sprintf or typecast)
- strpos the decimal
- grab the char from the next position

Cheers
Arno


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


DISCLAIMER=0A=
===========0A=
This e-mail may contain privileged and confidential information which is the=
property of Persistent Systems Ltd. It is intended only for the use of the=
individual or entity to which it is addressed. If you are not the intended=
recipient, you are not authorized to read, retain, copy, print, distribute=
or use this message. If you have received this communication in error, plea=
se notify the sender and delete all copies of this message. Persistent Syste=
ms Ltd. does not accept any liability for virus infected mails.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 12.10.2009 16:18:45 von Diogo Neves

--001636d34640892bb80475bd9b25
Content-Type: text/plain; charset=ISO-8859-1

A simple way to do that would be:

$elapsed = strval( 28.56018 );
$pos = strpos( $elapsed, '.' );
echo $elapsed[ ++$pos ];

On Sat, Jan 2, 2010 at 2:20 AM, Andre Dubuc wrote:

> Hi,
>
> I need to extract the first digit after the decimal point from a number
> such
> as 28.56018, which should be '5'.
>
> I've tried a few methods to accomplish this. If I use 'ini_set' I would
> need
> to know the number of digits before the decimal (which, unfortunately, I
> would not have access to).
>
> Then I've tried:
>
> >
> $elapsed = 28.56018;
>
> $digit = round($elapsed, 1); // rounds result is '6'
> $digit = number_format($elapsed, 1); // still rounds result to '6'
>
> ?>
>
> What I need is only the first digit after the decimal -- all the rest could
> be 'chopped' or discarded but without rounding the first digit after the
> decimal point.
>
> Is there any way of doing this?
>
> I'm stumped.
>
> Tia,
> Andre
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Thanks,

Diogo Neves
Web Developer @ SAPO.pt by PrimeIT.pt

--001636d34640892bb80475bd9b25--

RE: Need unrounded precision

am 12.10.2009 17:58:02 von Jaime Bozza

> -----Original Message-----
> From: Diogo Neves [mailto:dafneves@gmail.com]
> Sent: Monday, October 12, 2009 9:19 AM
> To: Andre Dubuc
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Need unrounded precision
>=20
> A simple way to do that would be:
>=20
> $elapsed =3D strval( 28.56018 );
> $pos =3D strpos( $elapsed, '.' );
> echo $elapsed[ ++$pos ];
>=20
> On Sat, Jan 2, 2010 at 2:20 AM, Andre Dubuc
> wrote:
>=20
> > Hi,
> >
> > I need to extract the first digit after the decimal point from a
> number
> > such
> > as 28.56018, which should be '5'.


Couldn't this be done with just simple math functions?


$a =3D 28.56018;
$b =3D intval(($a*10)-(intval($a)*10));

or:

$a =3D 28.56018;
$b =3D intval(($a-intval($a))*10);


Jaime


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Need unrounded precision

am 12.10.2009 18:13:00 von Andrea Giammarchi

--_698efd22-d0f1-45e8-a026-e5a741c3cebb_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



> Couldn't this be done with just simple math functions?

indeed:

$a =3D 28.56018=3B
$b =3D $a * 10 % 10 >> 0=3B

Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live Hotmail: Your friends can get your Facebook updates=2C right f=
rom Hotmail=AE.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_4:092=
009=

--_698efd22-d0f1-45e8-a026-e5a741c3cebb_--

RE: Need unrounded precision

am 12.10.2009 18:18:06 von Jaime Bozza

>> Couldn't this be done with just simple math functions?
>
>indeed:
>
>$a =3D 28.56018;
>$b =3D $a * 10 % 10 >> 0;

Hmmm... Didn't think about this, but % only works with int values, so

$b =3D $a * 10 % 10;

Should work as well.

Jaime


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Need unrounded precision

am 12.10.2009 18:26:44 von Andrea Giammarchi

--_42bb1c60-ec48-40f6-bd44-da51ff2efe5c_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable



> Hmmm... Didn't think about this=2C but % only works with int values

it was just future prof precaution since this statement is false for many o=
ther languages.
In few words I am not sure PHP6 does the same ... never mind so far

Regards
=0A=
____________________________________________________________ _____=0A=
Keep your friends updated=97even when you=92re not signed in.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_5:092=
010=

--_42bb1c60-ec48-40f6-bd44-da51ff2efe5c_--

RE: Need unrounded precision

am 12.10.2009 18:33:10 von Jaime Bozza

>> Hmmm... Didn't think about this, but % only works with int values
>
>it was just future prof precaution since this statement is false for many =
other languages.
>In few words I am not sure PHP6 does the same ... never mind so far

Good to know. In that case, I would probably just use intval() instead of =
>> since it's clearer and bitwise shifts aren't necessarily integer only ei=
ther.

Jaime


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Need unrounded precision

am 12.10.2009 21:37:30 von Andrea Giammarchi

--_bc107dbf-d3b8-4107-82d8-e0995973e1ac_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable


bitwise right shift is probably the fastest cast to int so far ... still in=
many languages=2C intval is a function call

being a cast in both cases (int) is good as well ... bitwise=2C casting=2C =
works with strings=2C arrays=2C boolean=2C whatever as well.

I don't think there is any difference in php=2C except when the integer is =
"too big" ... but this was not the case=2C we had to deal with 1 to 10 :-)

Regards



> From: jbozza@mindsites.com
> To: an_red@hotmail.com
> CC: php-general@lists.php.net
> Date: Mon=2C 12 Oct 2009 11:33:10 -0500
> Subject: RE: [PHP] Need unrounded precision
>=20
> >> Hmmm... Didn't think about this=2C but % only works with int values
> >
> >it was just future prof precaution since this statement is false for man=
y other languages.
> >In few words I am not sure PHP6 does the same ... never mind so far
>=20
> Good to know. In that case=2C I would probably just use intval() instead=
of >> since it's clearer and bitwise shifts aren't necessarily integer onl=
y either.
>=20
> Jaime
>=20
=0A=
____________________________________________________________ _____=0A=
Windows Live: Make it easier for your friends to see what you=92re up to on=
Facebook.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_2:092=
009=

--_bc107dbf-d3b8-4107-82d8-e0995973e1ac_--

Re: Need unrounded precision

am 14.10.2009 21:52:46 von Philip Thompson

On Oct 12, 2009, at 2:37 PM, Andrea Giammarchi wrote:

> bitwise right shift is probably the fastest cast to int so far ...
> still in many languages, intval is a function call
>
> being a cast in both cases (int) is good as well ... bitwise,
> casting, works with strings, arrays, boolean, whatever as well.
>
> I don't think there is any difference in php, except when the
> integer is "too big" ... but this was not the case, we had to deal
> with 1 to 10 :-)
>
> Regards
>
>> From: jbozza@mindsites.com
>> To: an_red@hotmail.com
>> CC: php-general@lists.php.net
>> Date: Mon, 12 Oct 2009 11:33:10 -0500
>> Subject: RE: [PHP] Need unrounded precision
>>
>>>> Hmmm... Didn't think about this, but % only works with int values
>>>
>>> it was just future prof precaution since this statement is false
>>> for many other languages.
>>> In few words I am not sure PHP6 does the same ... never mind so far
>>
>> Good to know. In that case, I would probably just use intval()
>> instead of >> since it's clearer and bitwise shifts aren't
>> necessarily integer only either.
>>
>> Jaime

I like the bitwise shifting, but here's another potential (albeit
probably slower) solution.

$a = 28.56018;
list (,$dec) = explode ('.', $a);
$b = $dec{0};

Cheers,
~Philip

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 16.10.2009 08:18:06 von Kim Madsen

Hello

Andre Dubuc wrote on 2010-01-02 02:20:
> Hi,
>
> I need to extract the first digit after the decimal point from a number such
> as 28.56018, which should be '5'.

Since no one came up with the simple solution:

$num = "28.56018";
ereg("^[0-9]+\.([0-9]){1}", trim($num), $regs);
if($regs[1])
$digit = $regs[1];
else
print "no digit found";

--
Kind regards
Kim Emax


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 04.11.2009 16:48:05 von Matthew McKay

Kim Madsen wrote:
> Hello
>
> Andre Dubuc wrote on 2010-01-02 02:20:
>> Hi,
>>
>> I need to extract the first digit after the decimal point from a
>> number such as 28.56018, which should be '5'.
>
> Since no one came up with the simple solution:
>
> $num = "28.56018";
> ereg("^[0-9]+\.([0-9]){1}", trim($num), $regs);
> if($regs[1])
> $digit = $regs[1];
> else
> print "no digit found";
>
My submission for a "simple" solution. I wish I had xslt2 =(


select="number(substring(substring-after(number(translate(no rmalize-space($number),translate(normalize-space($number),'. 0123456789',''),'')),'.'),1,1))"/>



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Need unrounded precision

am 04.11.2009 17:23:32 von Nathan Rixham

Matthew McKay wrote:
> Kim Madsen wrote:
>> Hello
>>
>> Andre Dubuc wrote on 2010-01-02 02:20:
>>> Hi,
>>>
>>> I need to extract the first digit after the decimal point from a
>>> number such as 28.56018, which should be '5'.
>>
>> Since no one came up with the simple solution:
>>
>> $num = "28.56018";
>> ereg("^[0-9]+\.([0-9]){1}", trim($num), $regs);
>> if($regs[1])
>> $digit = $regs[1];
>> else
>> print "no digit found";
>>
> My submission for a "simple" solution. I wish I had xslt2 =(
>
>
> > select="number(substring(substring-after(number(translate(no rmalize-space($number),translate(normalize-space($number),'. 0123456789',''),'')),'.'),1,1))"/>
>
>

>

nice to see a bit of xslt w/ functions :) just for the hell of it here's
one off the top of my head

$num = 28.56018;
substr((int)($num*10) , -1); #5

and one without any functions..

(int)(($num-(int)$num)*10); #5




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php