How to make a number 8 digit as standard digit. The number will be get to a user id from database.
Example
user_id = 1 // This should should be echo as 00000001
user_id = 11 // This should should be echo as 00000011
user_id = 111 // This should should be echo as 00000111
How can I code this? Please help thanks.
You can use printf function with %08s as the format string:
printf("%08s",$user_id);
If you want to store the result back in the string you can use sprintf as:
$user_id = sprintf("%08s",$user_id);
The format specifier %08s says:
s : Interpret the argument as a string
8 : Print the string left justified within 8 alloted places
0 : Fill the unused places with 0
You could use printf:
printf("%08d", $user_id);
PHP has sprintf:
$user_str = sprintf("%08d", $user_id);
echo $user_str;
You can do with str_pad
echo str_pad($user_id,8,'0',STR_PAD_LEFT);
$user_id = str_pad($user_id, 8, "0", STR_PAD_LEFT);
function leadingZeroes($number, $paddingPlaces = 3) {
return sprintf('%0' . $paddingPlaces . 'd', $number);
}
Source.
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
I have a value in database which contains 20 numbers, example :
1.1234567891011223
I want to limit the echoed value to 1.123 or 1.12 only instead of this long number..
How can I do that in PHP?
It sounds like you're looking for either the round function:
$long_number = 1.1234567891011223;
$formatted_number = round($long_number, 2);
echo $formatted_number; // 1.12
Or the number_format function:
$long_number = 1.1234567891011223;
$formatted_number = number_format($long_number, 2, '.', '');
echo $formatted_number; // 1.12
Hope this helps! :)
use round function
echo round(1.1234567891011223,2); // output: 1.12
is this what you are searching for? http://php.net/manual/de/function.number-format.php
use it like that:
<?php
$a = 1.1234567891011223;
echo number_format ( $a , 3 , '.', '');
?>
Take a look at strpos and substr. Below is an example from another post #Nineoclick This function finds the decimal and rounds the number. Rounding the number will shorten it.
truncate(1.1234567891011223, 2)
function truncate($val,$p = 0)
{
$strpos = strpos($val, '.');
return $p < 0 ? round($val,$p) : ($strpos ? (float)substr($val,0,$strpos+1+$p) : $val);
}
You can also use sprintf for this
$formatted = sprintf("%01.2f", $longNum);
Or if you're just going to echo it and don't need the variable, printf
printf("%01.2f", $longNum);
How can I get the first and the last digit of a number? For example 2468, I want to get the number 28. I am able to get the ones in the middle (46) but I can't do the same for the first and last digit.
For the digits in the middle I can do it
$substrmid = substr ($sum,1,-1); //my $sum is 2468
echo $substrmid;
Thank you in advance.
You can get first and last character from string as below:-
$sum = (string)2468; // type casting int to string
echo $sum[0]; // 2
echo $sum[strlen($sum)-1]; // 8
OR
$arr = str_split(2468); // convert string to an array
echo reset($arr); // 2
echo end($arr); // 8
Best way is to use substr described by Mark Baker in his comment,
$sum = 2468; // No need of type casting
echo substr($sum, 0, 1); // 2
echo substr($sum, -1); // 8
You can use substr like this:
<?php
$a = 2468;
echo substr($a, 0, 1).substr($a,-1);
You can also use something like this (without casting).
$num = 2468;
$lastDigit = abs($num % 10); // 8
However, this solution doesn't work for decimal numbers, but if you know that you'll be working with nothing else than integers, it'll work.
The abs bit is there to cover the case of negative integers.
$num = (string)123;
$first = reset($num);
$last = end($num);
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: show a number to 2 decimal places
How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.
Here you go :)
echo number_format($var,2);
If you want it to print specific no. of decimal points, use number_format.
$float_var = number_format($var, 2);
$var = number_format($number, 2, '.', '');
This forces 2 points after the decimal, sets the decimal as a period. You can also forego the last two as it defaults to it;
Note: The third value is your decimal separator, the fourth value is the thousandths separator.
$var = number_format($number, 2);
For direct output:
printf('%0.2f',$var);
Output into variable:
$outVar = sprintf('%0.2f',$var);
This statemant casts $var type to float and prints with 2 decimal signs
maybe you should check it first if the value is not set
if(!isset($variableName))
{
// then set
$variableName = "0.00"; // => string
//or like this
$variableName = number_format(0,2); // => this result is also string
}
echo "value: ",$variableName;
result
0.00
if you are trying to format the value i sugest you to use meioMask pluging.
So you define your field as number and the pluging do the trick, even if you set "0" for the value