This question already has answers here:
Zero-pad digits in string
(5 answers)
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 9 years ago.
For example, I have this format:
$s_number = "12"; And for this input, I need 0012 as the output:
Another example:
Input $s_number = "3";
Output: 0003
Every time I need 4 digits I would like this to happen.
It won't be a number (but a string), but you can do that using str_pad. In your examples:
$s_number = str_pad( "12", 4, "0", STR_PAD_LEFT );
$s_number = str_pad( "3", 4, "0", STR_PAD_LEFT );
Use str_pad() for that:
echo str_pad($number, 4, '0', STR_PAD_LEFT);
You can achieve this easily by using phps' printf
<?php
$s_number = '12';
printf("%04d", $s_number);
?>
Hope this helps
Related
This question already has answers here:
MySQL custom primary key generator
(3 answers)
Closed 3 years ago.
I have a pattern of integer (0000012019). where 2019 is current year and 000001 need to increase with every new row inserted to db like 000002, 000003, 000004, 000005 and so on.
how can i achieve this?
First of all it's a string you have.
You can split the string with substr() and do the math on the first part and then convert back to string with str_pad by padding with 0's from left.
$str = '0000012019';
$first = substr($str, 0, 6);
$year = substr($str, -4);
$first = str_pad($first+1, 6, 0, STR_PAD_LEFT);
$new = $first . $year;
echo $new; // 0000022019
This question already has answers here:
How to make number_format() not to round numbers up
(18 answers)
Closed 3 years ago.
I have this number
$sku = '2200081005966';
and I want to convert the number like this without rounding the number
$new_sku = '5.96'
I already try this number_format(substr($sku, 7, 12), 0, '', '.')
but the output that I get 5.97.
Any ideas how can I make this work?
Thank you.
Add floor() around your number_format:
$sku = '2200081005966';
echo floor(number_format(substr($sku, 7, 12), 0, '', '.')*100)/100;
Outputs:
5.96
Note: It would works well in case of positive numbers, your substr always take a positive number, that's why it would be enough.
<?php
$sku = '2200081005966';
$foo=substr($sku, 9, 12);
echo substr($foo, 0,2).".".substr($foo, 3,3);
?>
first you get the last 4 charakters of the string, then you split that part and put a dot between.
This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
Hi all just a very quick one.
How would I write in an sql query or php to change the number of digits displayed.
For example I am using this $values['ClientId'] which is a AI primary key, I know that until I get to 10 it will look like 1, 2, 3, 4,...,10, but I want it to look like 01, 02, 03. or even 001.
Probably a real simple one but I c
ant find it.
Use the str_pad function:
http://php.net/manual/en/function.str-pad.php
str_pad( $number, $padLength, $padWith, STR_PAD_LEFT );
str_pad( "1", 4, "0", STR_PAD_LEFT ); // gives 0001
$input=1; //if you need 01 instated of 1 then try
echo sprintf("%02d", $input);
$input=1; //if you need 001 instated of 1 then try
echo sprintf("%03d", $input);
please read this sprintf
This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
I have a piece of code for converting a Decimal number into base 3
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo $base_three;
So the number that it echos is 101 and it has 3 digits.
but I what it to echos is 000101 so that it has 6 digits.
Converting Decimal into base 3 with always 6 digits even though it has only 3 or 4 useful digits, is my goal! how can I solve it ?
try this
echo str_pad($base_three, 6, "0", STR_PAD_LEFT);
You can use sprintf to ensure that it always has a total of 6 digits, with leading zeroes:
$base_three = 101;
$padded = sprintf("%06s", $base_three);
echo $padded;
Convert to a string and pad with 0's.
$test = str_pad($base_three, 6, '0', STR_PAD_LEFT);
echo $test;
http://php.net/manual/en/function.str-pad.php
You can use sprintf to make sure you always output 6 digits, whatever number you have:
$number = 010;
sprintf("%06d", $number);
so the complete piece of code would be:
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo sprintf("%06d", $base_three);
or
printf("%06d", $base_three);
printf formats the variable and echos it, sprintf() doesn't echo but returns it
(s)printf can do a lot more, see http://www.php.net/manual/en/function.sprintf.php
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have an integer that I need to convert into a 4 digits string. I know the integer number is between 1 and 9999. If the number is 4, I want the output string to be "0004". If the number is 134, I need the string output as "0134" and so on.
What would be the shortest most elegant way of achieving this in PHP? Thank you.
I would use sprintf():
$string = sprintf( "%04d", $number);
Using this demo:
foreach( array( 4, 134) as $number) {
$string = sprintf( "%04d", $number);
echo $string . "\n";
}
You get as output:
0004
0134
Try this
$num = 1;
$paddedNum = sprintf("%04d", $num);
echo $paddedNum;
Try this
str_pad($input, 4, "0", STR_PAD_LEFT);
This will work for integer and string both
http://php.net/manual/de/function.str-pad.php
$input = 9;
$str = str_pad($input, 4, "0", STR_PAD_LEFT); //results in 0009
You can use sprintf with the %d option:
$NewString = sprintf( "%04d", $OldNumber);
the 04 tells sprintf how many digits your number should be, and will fill with zeros if it doesn't reach that number.
$num = rand(1,9999);
echo sprintf( "%04d", $num);
Try this.