PHP中错误的浮动格式(sprintf,printf) [英] Wrong float formatting in PHP (sprintf, printf)

查看:213
本文介绍了PHP中错误的浮动格式(sprintf,printf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ a = 111749392891; 

printf('%f',$ a);
111749392890.000000

printf('%F',$ a);
111749392890.000000

printf('%F.2',$ a)
111749392890.:00000.2

printf('%F0。 2',$ a);
111749392890.:000000.2

number_format($ a,2,'。','');
111749392891.00

只有number_format()输出对我来说才算合适。我错过了什么吗?我正在使用PHP 5.3。

解决方案

您将格式类型修饰符放在格式类型说明符之后而不是之前。试试这个:

  printf('%。2F',$ a)

至于奇数输出,您的本地化设置可能会这样做。尝试运行下面的行,看看你的本地返回什么。

  echo setlocale(LC_ALL,null); 

尝试将您的语言环境更改为其他语言,以查看问题是否消失。像这样:

  setlocale(LC_ALL,'en_CA.UTF-8'); 


I was debugging PHP code and found out the following:

$a = 111749392891;

printf('%f', $a);
111749392890.:00000

printf('%F', $a);
111749392890.:00000

printf('%F.2', $a)
111749392890.:00000.2

printf('%F0.2', $a);
111749392890.:000000.2

number_format($a, 2, '.','');
111749392891.00

Only number_format() output looks OK to me. Am I missing something? I'm using PHP 5.3.

解决方案

You are placing the format type modifiers after the format type specifier instead of before. Try this:

printf('%.2F', $a)

As for the odd output, it is possible that your localization settings are doing that. Try running the line below and see what is returned for your local.

echo setlocale(LC_ALL, null);

Try changing your locale to something different to see if the problem goes away. Like so:

setlocale(LC_ALL, 'en_CA.UTF-8');

这篇关于PHP中错误的浮动格式(sprintf,printf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆