This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 3 years ago.
I'm trying to printf a variable with some 000 on the left side but only print the 000 and no print the variable.
$activePlayers1 = array(3)
numero = $activePlayers1[$i];
printf('000',$numero);
The result of this is 000 and no print the 3.
If you are trying to pad with zeros, you should use a padding specifier:
<?php
$numero = 5;
printf("%'.09d", $numero);
?>
yields
000000005
Try it yourself
The d is a type specifier, used to treat the variable value as an integer and display it as a decimal, and you can find the full list of type specifiers under the sprintf documentation.
d - the argument is treated as an integer and presented as a (signed)
decimal number.
It should be like this
$activePlayers1 = array(3)
numero = $activePlayers1[$i];
printf('%d',$numero);
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
<?php
$result = array_filter($_POST);
if(!empty($result)){
echo ((max($result)+min($result))/2)*1.2 ."|".((max($result)+min($result))/2)*1.3;
}
?>
hi all..how to set min and max result to 2 decimals places for the code above? actually the result will appears in input type text after the process complete
Use number_format function in PHP.
number_format ( float $number [, int $decimals = 0 ] ) : string
For more information see here
$yourNumber = 1235.343;
echo number_format ($yourNumber, 2);
// Will output 1,235.34 (with two decimals, specified in number_format as second paramter.
Edit: Max / Min functions return mixed value. Make sure its float and then pass it to number_format. It will returns you the string.
This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);
This question already has answers here:
Convert exponential number presented as string to a decimal
(3 answers)
Closed 5 years ago.
I am trying to convert a scientific notation/exponential number.
For example :
I get from a API request this number : 4.96E-6
I want to convert it to his normal value 0.00000496.
I searched on the web and tryed a few codes like
$awb = "4.96e-6"; // tried with and without quotes
$format = sprintf($awb, "%e");
var_dump($format);
it returns : string(7) "4.96E-6"
$awb = 4.96e-6;
$format = sscanf($awb, "%e");
var_dump($format);
Is returning :
array(1) { [0]=> float(4.96E-6) }
Please what should I do ?
Thank you
You can use the number_format() function; e.g.
<?php
$awb = "4.96e-6";
print number_format($awb, 10);
?>
Will result in the output: 0.0000049600.
See: http://php.net/manual/en/function.number-format.php
This question already has answers here:
PHP float with 2 decimal places: .00
(13 answers)
Closed 6 years ago.
When i convert number like 15.26 to float (i know it is already in float) it return 15.26 using below method
$float = (float) 15.26; //15.26 (data type float)
and it work perfect
but when i convert 15 to float it return only 15
$float = (float) 15; //15 (data type float)
instead of only 15 how to get "15.0" or "15.00" with float data type
You can use number_format function like this:
echo number_format(15,2);
// ^^ Your value and number of digits after fraction point
Read : number_format
Demo : https://3v4l.org/bY5Wo
This question already has answers here:
What's the function of the ~ bitwise operator (Tilde) [duplicate]
(3 answers)
Closed 9 years ago.
Consider:
php > $a = 12; // 1100
php > echo ~$a;
-13
I would expect the inverse of 1100 to be either 0011 (direct) or 11110011 (an entire byte). That would give a result to either 3 or 243. Whence cometh -13?
Again, for good measure, another unexpected result of the same type and explanation:
php > $b = 6; // 0110
php > echo ~$b;
-7
Why -7?
Look at this code:
<?php
$val = 6;
print "$val = ".decbin($val);
print "\n";
$val = ~$val;
print "$val = ".decbin($val);
It prints
6 = 110
-7 = 11111111111111111111111111111001
At first you have 110. As my php uses 32 bits, after inverting all the bits, we get this huge number. As the 1-st bit is 1, php interprets it as a negative value, stored, using two's-complement representation. To find out, the modulus of the negative value, stored in this notation, we
invert the digits:
110
add one to the result:
111
which gives us 7
So, the value is -7
http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
Why -7?
6 is 00000000000000000000000000000110, so ~6 is ~00000000000000000000000000000110, and that's equal to 11111111111111111111111111111001. Because a signed datatype is used, the first bit indicates whether the number is positive or negative (positive = 0 and negative = 1). Because it is Two's complement, you should convert the binary number to decimal using this way:
Invert the binary number. You get 00000000000000000000000000000110
Convert 00000000000000000000000000000110 (a positive binary number) to a decimal number. You get 6
Add 6 up with one: you get 7
Make it negative: you get -7