为什么用PHP printf()" g" type-specifier给出错误的值?

时间:2017-11-17 20:21:53

标签: php printf

根据documentation, " g - 较短的%e和%f"

我已经执行了这个陈述:

printf("e:%1\$.3e\nf:%1\$.3f\ng:%1\$.3g", .123e2);

这是输出:

e:1.230e+1 // long value
f:12.300 // shorter value
g:12.3 // here g gives something else

为什么"g"会提供其他内容,我的文档错误了吗?

$ php -v
PHP 7.1.10 (cli) (built: Oct 19 2017 15:16:13) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Xdebug v2.5.1, Copyright (c) 2002-2017, by Derick Rethans

1 个答案:

答案 0 :(得分:0)

sprintf()最终委托sprintf(3) C函数。从printf(3)手册页:

Precision

   [...] This gives ... the maximum number of significant digits for g and G 
   conversions...

因此,PHP文档充其量是不准确的。

相关问题