1

In PHP, when using microtime(true), I am expecting to get in most cases 6 digits to the right of the decimal.

<?php
//version 5.5.31

$time = microtime();
$timeTrue = microtime(true);

var_dump($time, $timeTrue);

// string(21) "0.64728900 1462577720"
// float(1462577720.6473)
?>

Why is the float only showing the first four digits? Sometimes I only get three digits, which makes sense. Should it not be returning float(1462577720.647289). ie 6 digits in most cases, occasionally 5 digits?

matt
  • 223
  • 4
  • 13

1 Answers1

2

You seem to be confusing precision with digits after the decimals point.

Double precision floating points usually have a maximum precision of 14 to 16 digits and your example shows 14. Yes, all digits count. The decimal point is handled by another variable (at the floating point definition level).

The same principle would apply to very large numbers which would also lose precision after 14-15 digits long.

Julie Pelletier
  • 1,740
  • 1
  • 10
  • 18
  • 1
    you can change php.ini's `precision` runtime with `ini_set('precision', 16);` – Federkun May 07 '16 at 00:21
  • Agreed. When I use ini_set("precision", 16) I do get more digits. I am not sure why PHP has a function return that gives less than the default precision; I would think in this case the function would internally return the required number of digits to be precise down the the millionth of a second for microtime. – matt May 07 '16 at 00:23
  • Meant 'needed precision' instead of 'default precision' – matt May 07 '16 at 00:36