2015-01-12 55 views
0

我正在運行無所作爲的基準測試,因此結果非常快。強制顯示常規十進制數

這裏是我的代碼:

$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將其顯示爲普通十進制數?

+0

PHP將切換到使用科學根據你的ini文件中的精度設置的不同格式:修改該設置,或使用[sprint()](http://www.php.net/manual/en/function.sprintf.php)強制非科學格式顯示 –

+0

[顯示浮點值不帶科學記數法]可能的重複(http://stackoverflow.com/questions/10916675/display-float-value-wo-scientific-notation) –

+0

類似的問題有很好的答案:http: //stackoverflow.com/a/10917464/19905 – ash108

回答

1

如果你想你的大數字,那就試試這個:

//$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

鐵道部的相關信息http://php.net/manual/en/function.gmp-strval.php

+0

看起來很有趣,但...致命的錯誤:調用未定義的函數gmp_init()在... – iprophesy

+0

我認爲這是我的PHP版本,但我會測試它在更高的版本... :)接受答案!這正是我所期待的! – iprophesy

1

可以使用printfsprintf功能。下面是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