I have the following code as the only way I know to convert a float to a string with the fewest possible significant digits required to reproduce it (dtoa()
with mode 4
in C).
$i = 14;
do {
$str = sprintf("%.{$i}e", $x);
$i++;
} while ($x != (float) $str);
The Hack typechecker reports an error because it expects the first parameter to sprintf()
to be a literal string so it can check it against the arguments. Is there a way I can turn that off for this line?
Or is there another way I could achieve the same thing? Perhaps with the NumberFormatter
class?