PHP string Formatting - php

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

Related

Get the number before dot symbol

How to get the number before dot symbol, i try using round() but sometimes give me wrong number for what i need, i have the following calculation, lets say
$Calc = 10/3;
$Calc = 3.333333333333333;
I'm trying to use round() php function
$Calc = 10/3;
$Calc = round(3.333333333333333);
$Calc = 3;
but for example if my result is 3.51+
$Calc = round(3.51);
$Calc = 4;
BUT i just need the 3, not the rest , how can i get that?
you could try with intval() function
echo intval(3.51); // output will be 3
echo intval(10/3); // output will be 3
Use this code:
$Calc = 10/3;
$pos = strpos($Calc, '.');
$Calc = substr($Calc, 0, $pos);
This will give the all the digits before the decimal.
Use floor:
echo floor(3.99); // 3
echo floor(3.023);// 3
echo floor(4.4); // 4
echo floor(9.0); // 9

Adding a string and an integer in php

I'm trying to add a 1 in front of my binary code and this is how I'm going about it:
if I have 0101, for example, then I'd add a number with 4 zeroes, like 10000 so it would become 10101. Here's my code:
$fill = strlen($string);
$number = '1';
$add = str_pad($number, $fill, '0', STR_PAD_RIGHT);
$m1 = $string + $add;
The problem is the output for that is something like 1.random number e+Random number
assuming $string is your "0101" string, you could just do $m1 = '1'.$string;
My previous answer was wrong because the length of the string is potentially variable and str_pad requires you to know the length. This will work, but it doesn't look so elegant:
if (strpos($string, '0') === 0) {
$string = '1' . $string;
}

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 do i use 4 digit numbers

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

Formatting a number with leading zeros in PHP [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed 2 years ago.
I have a variable which contains the value 1234567.
I would like it to contain exactly 8 digits, i.e. 01234567.
Is there a PHP function for that?
Use sprintf :
sprintf('%08d', 1234567);
Alternatively you can also use str_pad:
str_pad($value, 8, '0', STR_PAD_LEFT);
Given that the value is in $value:
To echo it:
printf("%08d", $value);
To get it:
$formatted_value = sprintf("%08d", $value);
That should do the trick
When I need 01 instead of 1, the following worked for me:
$number = 1;
$number = str_pad($number, 2, '0', STR_PAD_LEFT);
echo str_pad("1234567", 8, '0', STR_PAD_LEFT);
sprintf is what you need.
EDIT (somehow requested by the downvotes), from the page linked above, here's a sample "zero-padded integers":
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
Though I'm not really sure what you want to do you are probably looking for sprintf.
This would be:
$value = sprintf( '%08d', 1234567 );
Simple answer
$p = 1234567;
$p = sprintf("%08d",$p);
I'm not sure how to interpret the comment saying "It will never be more than 8 digits" and if it's referring to the input or the output. If it refers to the output you would have to have an additional substr() call to clip the string.
To clip the first 8 digits
$p = substr(sprintf('%08d', $p),0,8);
To clip the last 8 digits
$p = substr(sprintf('%08d', $p),-8,8);
If the input numbers have always 7 or 8 digits, you can also use
$str = ($input < 10000000) ? 0 . $input : $input;
I ran some tests and get that this would be up to double as fast as str_pad or sprintf.
If the input can have any length, then you could also use
$str = substr('00000000' . $input, -8);
This is not as fast as the other one, but should also be a little bit faster than str_pad and sprintf.
Btw: My test also said that sprintf is a little faster than str_pad. I made all tests with PHP 5.6.
Edit: Altough the substr version seems to be still very fast (PHP 7.2), it also is broken in case your input can be longer than the length you want to pad to. E.g. you want to pad to 3 digits and your input has 4 than substr('0000' . '1234', -3) = '234' will only result in the last 3 digits
$no_of_digit = 10;
$number = 123;
$length = strlen((string)$number);
for($i = $length;$i<$no_of_digit;$i++)
{
$number = '0'.$number;
}
echo $number; /////// result 0000000123
I wrote this simple function to produce this format: 01:00:03
Seconds are always shown (even if zero).
Minutes are shown if greater than zero or if hours or days are required.
Hours are shown if greater than zero or if days are required.
Days are shown if greater than zero.
function formatSeconds($secs) {
$result = '';
$seconds = intval($secs) % 60;
$minutes = (intval($secs) / 60) % 60;
$hours = (intval($secs) / 3600) % 24;
$days = intval(intval($secs) / (3600*24));
if ($days > 0) {
$result = str_pad($days, 2, '0', STR_PAD_LEFT) . ':';
}
if(($hours > 0) || ($result!="")) {
$result .= str_pad($hours, 2, '0', STR_PAD_LEFT) . ':';
}
if (($minutes > 0) || ($result!="")) {
$result .= str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':';
}
//seconds aways shown
$result .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $result;
} //funct
Examples:
echo formatSeconds(15); //15
echo formatSeconds(100); //01:40
echo formatSeconds(10800); //03:00:00 (mins shown even if zero)
echo formatSeconds(10000000); //115:17:46:40
You can always abuse type juggling:
function zpad(int $value, int $pad): string {
return substr(1, $value + 10 ** $pad);
}
This wont work as expected if either 10 ** pad > INT_MAX or value >= 10 * pad.

Categories