开发者

base_convert and negative numbers

开发者 https://www.devze.com 2023-03-27 17:31 出处:网络
The base_convert() function doesn\'t seem to preserve the sig开发者_如何学JAVAn. For example: var_dump (base_convert (\'-100\', 10, 10));

The base_convert() function doesn't seem to preserve the sig开发者_如何学JAVAn.

For example:

var_dump (base_convert ('-100', 10, 10));

The output of this is 100

Is there a way of converting bases without losing the sign?


I did not see a PHP standard function to do so, however you could write your own.

function signed_base_convert($number, $src_base, $dest_base)
{
    $sign = (intval($number, $src_base) >= 0 ? '' : '-');
    return $sign . base_convert($number, $src_base, $dest_base);
}

I do not have access to PHP at the moment to test this, but it should give you a good idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消