强制显示常规十进制数
Force to display regular decimal number
我是 运行 什么都不做的基准测试,所以结果很快。
这是我的代码:
$time_start = microtime(true);
//Do Nothing...
$time = microtime(true) - $time_start;
echo 'Took '.$time.' seconds<br>';
问题是当我尝试回应我得到的结果时:
Took 1.3828277587891E-5 seconds
我希望得到一个常规的十进制数,例如:
Took 0.000000008231 seconds
是否可以强制 php 将其显示为常规十进制数?
如果你想要大数字,试试这个:
//$i = gmp_init( $time ); // i think you need that only if you want convert a string to an int/flaot
echo gmp_strval( $time );
gmp_strval PHP > 4.0.4 / PHP 5.
您可以使用printf
或sprintf
函数。这是来自 http://php.net/manual/en/function.sprintf.php
的样本
<?php
$n = 43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
printf("%%b = '%b'\n", $n); // binary representation
printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'\n", $n); // standard integer representation
printf("%%e = '%e'\n", $n); // scientific notation
printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer
printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer
printf("%%f = '%f'\n", $n); // floating point representation
printf("%%o = '%o'\n", $n); // octal representation
printf("%%s = '%s'\n", $n); // string representation
printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)
printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer
对于您的示例,您可以使用 - 例如 - :
<?php
$time_start = microtime(true);
//Do Nothing...
$time = microtime(true) - $time_start;
echo 'Took '.sprintf("%f",$time).' seconds<br>';
您甚至可以通过这种方式更改精度:
sprintf("%.1f",$time) // -> 0.0 seconds
sprintf("%.10f",$time) // -> 0.0000059605 seconds
你可以使用 number_format()
检查 http://php.net/number_format
我是 运行 什么都不做的基准测试,所以结果很快。
这是我的代码:
$time_start = microtime(true);
//Do Nothing...
$time = microtime(true) - $time_start;
echo 'Took '.$time.' seconds<br>';
问题是当我尝试回应我得到的结果时:
Took 1.3828277587891E-5 seconds
我希望得到一个常规的十进制数,例如:
Took 0.000000008231 seconds
是否可以强制 php 将其显示为常规十进制数?
如果你想要大数字,试试这个:
//$i = gmp_init( $time ); // i think you need that only if you want convert a string to an int/flaot
echo gmp_strval( $time );
gmp_strval PHP > 4.0.4 / PHP 5.
您可以使用printf
或sprintf
函数。这是来自 http://php.net/manual/en/function.sprintf.php
<?php
$n = 43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
printf("%%b = '%b'\n", $n); // binary representation
printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'\n", $n); // standard integer representation
printf("%%e = '%e'\n", $n); // scientific notation
printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer
printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer
printf("%%f = '%f'\n", $n); // floating point representation
printf("%%o = '%o'\n", $n); // octal representation
printf("%%s = '%s'\n", $n); // string representation
printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)
printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer
对于您的示例,您可以使用 - 例如 - :
<?php
$time_start = microtime(true);
//Do Nothing...
$time = microtime(true) - $time_start;
echo 'Took '.sprintf("%f",$time).' seconds<br>';
您甚至可以通过这种方式更改精度:
sprintf("%.1f",$time) // -> 0.0 seconds
sprintf("%.10f",$time) // -> 0.0000059605 seconds
你可以使用 number_format() 检查 http://php.net/number_format