Re: Thousands Separator
| From: | Christoph Becker | Date: | Sat, 17 Jan 2015 01:40:56 +0000 |
| Subject: | Re: Thousands Separator | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324267@lists.php.net to get a copy of this message | ||
Jeffry Killen wrote:
> On Jan 16, 2015, at 3:37 PM, Christoph Becker wrote:
>
>> Jeffry Killen wrote:
>>
>>> Sorry, there is a misunderstanding on my part here:
>>> On Jan 16, 2015, at 2:31 PM, Jeffry Killen wrote:
>>>
> Actually what should work with settype is
>>>> $num = '123_456_789';
>>>> settype(str_replace('_', '', $num), 'int')
>>>>
> settype should coerce the data type in place to an int. (of float if you
> use 'float' as the second argument to settype
settype()'s first parameter is supposed to be passed by reference, but
values can't generally be passed by reference.
> I got this to work
> <?php
>
> $num = '123_456_789';
> $num = str_replace('_', '', $num);
> settype($num, 'int');
> print "\$num += 1: ".$num += 1;
> ?>
Yes, that works because you're passing a variable as first argument to
settype(). However, in this special case you don't need settype() at
all; the following has the same result:
<?php
$num = '123_456_789';
$num = str_replace('_', '', $num);
print "\$num += 1: ".$num += 1;
That is because the addition operator (+) "juggles" its operands to
numeric values, anyway. If you would replace the print statement with a
call to var_dump(), like so:
var_dump($num);
calling settype() before would make a difference, though.
--
Christoph M. Becker