php-general Digest 2 Feb 2013 18:43:29 -0000 Issue 8110
Topics (messages 320143 through 320146):
Re: Integer
320143 by: tamouse mailing lists
320144 by: Adam Richardson
320145 by: tamouse mailing lists
320146 by: Shawn McKenzie
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
On Fri, Feb 1, 2013 at 10:40 PM, Ron Piggott
<ron.pigg...@actsministries.org> wrote:
> In the following the “2.” means a moderator response and “25” is the account
> # of the moderator.
>
> <?php
>
> $author = 2.0000000000000000000000025
>
> ?>
>
> How can I get the 25 by itself?
> - I want to drop the “2.” and remove all the zero’s
>
> Would it be best to turn this into a STRING?
>
> Any suggestions?
Storing data as a float is a bad idea. Way bad. Also, I think that's
too many significant digits for php to handle in a float.
You should store data in an accessible way. For instance, assuming
there are actually only two pieces of interesting data there, you
could store them as a hash:
php > $author = Array('account' => 25, 'response' => 2);
php > var_dump($author);
array(2) {
["account"]=>
int(25)
["response"]=>
int(2)
}
If that data is coming from somewhere beyond your control and is
presented as that, then parsing as a string will be the way to go.
Again, assuming there is no other useful information, a simple regexp
to parse it would be:
php > if (preg_match('/(\d+)\.0+(\d+)/','2.0000000000000000000000025',$matches))
{
php { $author = Array('account' => $matches[2], 'response' => $matches[1]);
php { }
php > // else handle error if need be
php > var_dump($author);
array(2) {
["account"]=>
string(2) "25"
["response"]=>
string(1) "2"
}
--- End Message ---
--- Begin Message ---
On Fri, Feb 1, 2013 at 11:40 PM, Ron Piggott <ron.pigg...@actsministries.org
> wrote:
> How can I get the 25 by itself?
> - I want to drop the “2.” and remove all the zero’s
>
> Would it be best to turn this into a STRING?
>
I would recommend turning it into a string, splitting the string at the
decimal, then scanning the second string for the first non-zero and storing
the subsequent characters. Working with floats at that precision level
would likely lead to tragic results:
"10.0 times .1 is hardly ever 1.0"
http://www.eg.bucknell.edu/~xmeng/Course/CS2330/Handout/StyleKP.html
Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com
--- End Message ---
--- Begin Message ---
On Fri, Feb 1, 2013 at 11:27 PM, tamouse mailing lists
<tamouse.li...@gmail.com> wrote:
> On Fri, Feb 1, 2013 at 10:40 PM, Ron Piggott
> <ron.pigg...@actsministries.org> wrote:
>> In the following the “2.” means a moderator response and “25” is the account
>> # of the moderator.
>>
>> <?php
>>
>> $author = 2.0000000000000000000000025
>>
>> ?>
>>
>> How can I get the 25 by itself?
>> - I want to drop the “2.” and remove all the zero’s
>>
>> Would it be best to turn this into a STRING?
Sorry for complicating things. I didn't read your request carefully enough.
YES, treat it as a string, and if you just need the last 2 digits,
just grab the substring:
php > $author = substr('2.0000000000000000000000025',-2);
php > echo $author;
25
--- End Message ---
--- Begin Message ---
On 02/01/2013 10:40 PM, Ron Piggott wrote:
> In the following the “2.” means a moderator response and “25” is the account
> # of the moderator.
>
> <?php
>
> $author = 2.0000000000000000000000025
>
> ?>
>
> How can I get the 25 by itself?
> - I want to drop the “2.” and remove all the zero’s
>
> Would it be best to turn this into a STRING?
>
> Any suggestions?
>
>
> Ron Piggott
>
>
>
> www.TheVerseOfTheDay.info
>
Yeah needs to be a string:
$author = "2.0000000000000000000000025";
list($a, $b) = explode('.', $author);
echo (int)$a . ' ' . (int)$b;
--- End Message ---