how do i use 4 digit numbers - php

I need to count from:
0-9999
In PHP, how can i make the format:
0000-9999
So that the output is:
0000,0001,0002,....
thanks!

$num = 9;
$paddedNum = sprintf("%04d", $num);
echo $paddedNum; // prints 0009

$number = 9;
echo str_pad($number, 4, '0', STR_PAD_LEFT); // output: 0009

Related

PHP Calculation with 0 in front of variable value

How to allow PHP calculation 0 appears if example :
$a = "00001";
$b = "00005";
$total = $a + $b;
Expected result is : 00006
Including and keep 0000 appears.
I tried but the result always : 6
Just do normal math and then add the zeros back in front at the end:
$a = "00001";
$b = "00005";
$total = $a + $b;
$total = sprintf("%05d", $total );
or
$total = str_pad($total , 5, "0", STR_PAD_LEFT);
You might want to try and prepend the needed 0's
<?php
$total = 6;
$total_padded = sprintf("%05s", $num);
echo $num_padded; // returns 00006
?>
I haven't tested it but it should work :)
<?php
str_pad($total, 5, '0', STR_PAD_LEFT);
?>
I assume that the value is integer so you should use intval. If it is float use floatval
$a = "00001";
$b = "00005";
print intval($a);
print intval($b);
print intval($a)+intval($b);
than add the leading zeros
<?
str_pad($num, 5 , '0', STR_PAD_LEFT);
?>

PHP string Formatting

Using PHP I want to format a number(7) like in 6 digits 000007
I had done this via printf Function but wanted to store it in a variable how can I do so.
Below mentioned code not working, giving output as :000007 and printing it on screen:
$gensubjectid=printf("%06d\n", $origionalsubjectid);
Suggest.
Use sprintf - it's identical to printf but it returns the formatted string.
You can use: spirntf
$unformattedNumber = 7;
$formattedNumber = sprintf('%06d', $unformattedNumber);
Or you can try this (str_pad):
$input = 7;
str_pad($input, 6, "0", STR_PAD_LEFT);
Use str_pad().
$invID = str_pad($invID, 10, '0', STR_PAD_LEFT);
Or
Use sprintf: http://php.net/function.sprintf
$number = 51;
$number = sprintf('%10d',$number);
print $number;
// outputs 0000000051
$number = 8051;
$number = sprintf('%10d',$number);
print $number;
// outputs 0000008051

how to remove more 1 decimal numbers on a number

What's the simple way to remove more than 1 decimal number from source number .
for example source numbers are :
1st source number is : 56.48216585224
2nd source number is: 93
Output must be :
1st output : 56.4
2nd output: 93
numbers are not static
what's the simple way ?
If you don't want rounding, then:
$number = 56.48216585224;
echo substr($number, 0, strpos($number, '.')+2); // output: 56.4
Otherwise:
Use php round() or number_format()
http://php.net/manual/en/function.round.php
http://php.net/manual/en/function.number-format.php
Examples:
$number = 56.48216585224;
echo number_format($number, 1, '.', ''); // Output: 56.5
echo round($number, 1); // Output: 56.5
I will suggest this PHP code for your requirement:
$n = 56.48216585224;
$m = floor($n * 10) / 10; // $m = 56.4 now
You can try the following
print(quickFormat("56.48216585224"));
print(quickFormat("93"));
function quickFormat($number) {
$number = explode(".", $number);
if (isset($number[1])) {
return $number[0] . "." . $number[1]{0};
} else {
return $number[0] ;
}
}
Output
56.4
93

PHP set int length

Alright simple question
I have integriters like
5
188
4634
And they all need to be formated to
0000000005
0000000188
0000004634
It can become a string that doesnt matter.
sprintf is the function for that:
$num = sprintf("%010d", $num);
str_pad
echo str_pad($str, 10, "0",STR_PAD_LEFT);
<?php
#how many chars will be in the string
$filltotal = 10;
$number = 5;
#with str_pad function the zeros will be added
echo str_pad($number, $fill, '0', STR_PAD_LEFT);
// The result: 0000000005
Alternative is str_pad:
echo str_pad($num, 10, "0", STR_PAD_LEFT);
you can achieve by this.
$val = 12;
for($i=0;$i<(10 - count($val));$i++)
{
$str .= '0';
}
$final_val = $str.$val;

How to create array for possible probability (combinations) for given number in php

I am having mathematics oriented question.Here i like to get the all possible combinations
and looking to store in array.
For example:-
1 digit = 10 (0,1,2,3,....9)
2 digit = 100 (00,01,02,03,...)
I am having formula to find number of possibilities i.e 10^n -1.But i don't know how can i get the values.
function get_combinations(5){
//10^5 -1 = 99999 values
}
the function result should be
00000,00001,00010,00100,....
in array
not like
0,1,2,....00,01,03,...99999
EDIT
I also like to mix some alphabets with the number
results like
0000a,000a1,000z1,00001,00000,....
Thanks in advance
$n = ???;
$array = range(0, pow(10, $n)-1);
yes, there is no integer start with zero
so, dun bother to construct an array start with leading zero,
when you need to output str_pad can be applied
function get_combinations($n)
{
return range(0, pow(10, $n)-1);
}
$array = get_combinations(5);
$pad = strlen(max($array));
// to output
echo str_pad($array[0], $pad, 0, STR_PAD_LEFT);
$result = array();
$count = pow(10,$x);
for ($i = 0, $i < $count, $i++) {
$result[] = str_repeat('0', $x - strlen($i)) . $i;
}
You can't have an integer that is 0000 because that is just 0.
To get all numbers from 0 to 9 use the range() function:
$array = range(0,9); // array(0,1,2,3,4,5,6,7,8,9)
And alike for bigger numbers.
If you want them as strings use sprintf or related to reformat that array.
Or just use a for( loop :)
How about:
$n = 3;
function format($x) {
global $n;
return sprintf("%0${n}d", $x);
}
$arr = array_map('format', range(0, pow(10,$n)-1));
function get_combinations($exp){
$max = pow(10, $exp) - 1;
for ($i = 0; $i <= $max; $i++) $comb[] = str_pad($i, $exp, '0', STR_PAD_LEFT);
return $comb;
}

Categories