开发者

problem with printf() function

开发者 https://www.devze.com 2022-12-23 12:24 出处:网络
i have the variable $a = 5; i want to show it in binary form, with length, equal 8, with writing * at white spaces, just like this,like this * * * * *101

i have the variable $a = 5; i want to show it in binary form, with length, equal 8, with writing * at white spaces, just like this, like this * * * * *101


here the script

$number = 5;
printf("%*8b", $number); 

it doesn't work with *, but if "0"-s it works

   printf("%08b", $number);
      //returns 0000开发者_开发知识库0101

why it doesn't work with *?

EDIT:

and how can i apply the floating option too? like

`printf("%.4f", $number);` 

    //it returns 5.0000, but i want to return *****101.0000


http://www.php.net/manual/en/function.sprintf.php

An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). See the examples below.

So, use

printf("%'*8b", $number); 

EDIT:

for float try

printf("%'*.4f", $number); 

and if you want some more complicated cases, just read http://www.php.net/manual/en/function.sprintf.php

0

精彩评论

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