I was wondering if it is possible to always force a minimum of triple digits from an SQL statement to PHP, I am resonably new to PHP/SQL.
$count = $con->num_rows #num_rows currently shows 1
echo "Number of Rows: " .$count #shows "Number of Rows: 1"
How would I force it to output "Number of Rows: 001".
But then once it goes into natural four digit numbers, 1000, then it doesn't force triple digits.
Examples: 001 010 100 1000
Thanks for the help!
str_pad ( $count, 3, "0", STR_PAD_LEFT)
$newstring = str_pad($string,3,"0",STR_PAD_LEFT);
str_pad — Pad a string to a certain length with another string
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
and more
http://php.net/manual/en/function.str-pad.php is what you need.
Related
After a few calculations I get:
$int = 14.285714285714;
How can I take only the first four digits? Expected output: 14.28
Doing this with string functions is absolutely the wrong way to go about this, and these nearly identical answers look pretty spammy. If you want to round a number, round a number!
$int = round(14.285714285714, 2);
To truncate (as opposed to rounding), floor is the correct function in PHP:
$int = floor(14.285714285714 * 100) / 100;
Both work without any type conversions or casting.
Also note that a number with decimal places is categorically not an integer.
use substr function
$int = 14.285714285714;
echo substr($int, 0, 5);
$newint = (float)substr($int, 0, 5);
IF you want to round the number you can use
round($int, 2);
OUTPUT WILL BE : 14.29
LINK HOW TO ROUND
Try number format,
string number_format ( float $number [, int $decimals = 0 ] )
so
$int = 14.285714285714;
$small = (float)number_format ( $int ,2,'.', '' ); //gets rid of the "," for thousand separator
http://us2.php.net/manual/en/function.number-format.php
floor(100 * 14.285714285714) / 100
14.28
$caseid is (usually) a 5-digit number (it could be lower). I want a leading 0 so that the number is 6 digits long:
<?php
$caseid=12345;
echo str_pad(strval($caseid), 6-strlen(strval($caseid)), '0', STR_PAD_LEFT); ?>
This doesn't work as expected (displays the same number). Instead if I add 5 to the second argument of str_pad, it works.
<?php echo str_pad(strval($caseid), 11-strlen(strval($caseid)), '0', STR_PAD_LEFT); ?>
It works. What is the error here?
You don't need to compute the difference you just put the total characters you want it to be.
str_pad(strval($caseid), 6, '0', STR_PAD_LEFT);
I was once bothered by str_pad()s behavior too. I got tired of it, and returned to old stable sprintf().
<?php
header('Content-Type: text/plain');
$test = [
1,
12,
123,
1234,
12345,
123456
];
foreach($test as $n){
echo sprintf('%06d' . PHP_EOL, $n);
}
?>
Result:
000001
000012
000123
001234
012345
123456
Maybe, it is not an answer, but I hope it might help somebody.
See the function prototype:
string str_pad (
string $input ,
int $pad_length
[, string $pad_string = " "
[, int $pad_type = STR_PAD_RIGHT ]]
)
Pad a string to a certain length with another string, $pad_length means the length you want.
str_pad in php.net
The number is 13911392101301011 and regardless of using sprintf or number_format i get the same strange result.
sprintf('%017.0f', "13911392101301011"); // Result is 13911392101301012
number_format(13911392101301011, 0, '', ''); // Result is 13911392101301012
sprintf('%017.0f', "13911392101301013"); // Result is 13911392101301012
number_format(13911392101301013, 0, '', ''); // Result is 13911392101301012
As you actually have the number as a string, use the %s modifier:
sprintf('%s', "13911392101301011"); // 13911392101301011
Note that PHP is using a signed integer internally. The size depends on your system.
32bit system:
2^(32-1) = 2147483648
64bit system:
2^(64-1) = 9223372036854775808
-1 because 1 bit is reserved for the signage flag.
Since you are dealing with large numbers here, you may want to keep them as strings and perform numerical operation on the string values using BCMath functions.
$val = "13911392101301011";
echo $val; // 13911392101301011
echo bcadd($val, '4'); // 13911392101301015
echo bcmul($val, '2'); // 27822784202602022
You can do easily this way :-
ini_set("precision",25); // change 25 to whatever number you want or need
$num = 13911392101301011;
print $num;
Documentation states that $number in number_format is float so there is explicit typecast. Equivalent would look like this:
sprintf('%017.0f', (float) "13911392101301011");
Float is precise to around 14 digits and your number has 17 digits.
Your number_format call is setting the . and , to blank
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
try this:
number_format(13911392101301011, 0, '.', ',');
I have this string :
000000000000100
and need to convert it to:
1,00
So, the rules are:
Divide the number by 100 and use a comma as decimal separator
Strip leading zeros
Keep two decimals
From the PHP Manual page on number_format:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
If you want numbers like 123456 be formatted as 1234,45, use:
echo number_format($number / 100, 2, ",", "");
If you need a dot as thousands separator (1.234,56):
echo number_format($number / 100, 2, ",", ".");
The zeros are automatically removed by PHP when converting the string to a number.
string number_format ( float $number ,
int $decimals = 0 ,
string $dec_point = '.' ,
string $thousands_sep = ',' )
Manual: http://php.net/manual/en/function.number-format.php
// divide by 100 to shift ones place.
echo number_format((int)'000000000000100' / 100,2,',','');
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 4 years ago.
PHP - Is there a quick, on-the-fly method to test for a single character string, then prepend a leading zero?
Example:
$year = 11;
$month = 4;
$stamp = $year.add_single_zero_if_needed($month); // Imaginary function
echo $stamp; // 1104
You can use sprintf: http://php.net/manual/en/function.sprintf.php
<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>
It will only add the zero if it's less than the required number of characters.
Edit: As pointed out by #FelipeAls:
When working with numbers, you should use %d (rather than %s), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.
For example:
sprintf("%04s", 10); returns 0010
sprintf("%04s", -10); returns 0-10
Where as:
sprintf("%04d", 10); returns 0010
sprintf("%04d", -10); returns -010
You can use str_pad for adding 0's
str_pad($month, 2, '0', STR_PAD_LEFT);
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
The universal tool for string formatting, sprintf:
$stamp = sprintf('%s%02s', $year, $month);
http://php.net/manual/en/function.sprintf.php