Incorrect float formatting in PHP (sprintf, printf)

I debugged the PHP code and figured 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 the output of number_format () looks fine to me. Am I missing something? I am using PHP 5.3.

+4
source share
1 answer

You put format type modifiers after the format type specifier, not before. Try the following:

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

As for the odd output, it is possible that your localization settings do this. Try running the line below and see what returns for your local one.

 echo setlocale(LC_ALL, null); 

Try changing your language to something else to see if the problem goes away. For instance:

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

Source: https://habr.com/ru/post/1343574/


All Articles