This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
simple regex in php, formatting decimal number
How to convert a digit in x format to x.xx format in php?
example :
I have a number 5. Need to convert to 5.00
$n = 5;
$n = number_format($n, 2, '.', '');
You should try number_format():
$your_number = 5;
echo number_format($your_number, 2); // displays 5.00
Manual
See http://php.net/manual/en/function.number-format.php
number_format('your digit', 2 or 1, '.', '');
$number = 125
$no = number_format($number, 2, '.', '');
output = 125.00
$x = 5;
$x = sprintf('%.2f', $x);
echo $x;
Related
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.
This question already has answers here:
What is the best way to validate a credit card in PHP?
(8 answers)
Closed 7 years ago.
so tonight i was trying to create a function that will check if a credit card is valid or not.
However i'm stuck here.
In my calcul, i get number such as 10, 56, 30... number with 2 numbers.
(I mean, 1 is a number with 1 number just like 2, 3, 4 ,5 6, 7 , 8 ,9.. number with two numbers would be 10 ans higher.)
What I need to do is :
Get the first number and add it to a new variable, and do the same thing with another variable.
Example:
I have this number -> 23
I need to :
$var1 = 2;
$var2 = 3;
I wanted to use the function subtr, but it looks like it doesn't works with numbers ..
Thanks for reading !!
I hope you get something from this. Casting the number into a string first and then split the number using substr() after that cast the splitted value to integer again:
$num = 23;
$str_num = (string)$num;
$var1 = (int)substr($str_num, 0, 1);
$var2 = (int)substr($str_num, 1, 1);
Or using a pure numbers:
$num = 23;
$var2 = $num % 10;
$var1 = ($num - $var2) / 10;
Credit card numbers can be validated using an algorithm called the Luhn Algorithm.
If you need this in a project, don't reinvent the wheel. Check out this project on github.
Here's a way to do this using purely numbers (without ever casting to strings). This'll also work on any length of numbers assigning it to $var1, $var2, ... , $varn for n length number.
$num = 23;
$count = 1;
while ($num > 0) {
$var = "var".$count++;
$$var = $num % 10;
$num = intval($num / 10);
}
a numeric solution
$num = 23;
$var1 = floor($num / 10);
$var2 = $num % 10;
echo "$var1 $var2";
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
<?php
$str = "02";
$int = 1;
$result = $str-$int;
echo $result // 1
?>
But I need result = 01
Don't tell me "0".$str-$int;
<?php
$str = "02";
$int = 1;
printf('%02d', $str-$int);
or
<?php
$str = "02";
$int = 1;
$result = sprintf('%02d', $str-$int);
// do something with $result here
see http://docs.php.net/manual/en/function.sprintf.php
try
printf("%02d",$str-$int);
The explanation of the numerous formatting possibilities of printf are explained in the sprintf docs
<?
$str = "02";
$int = 1;
echo sprintf("%02d", (int)$str - $int);
?>
Use str_pad() where you set it to pad the left with 0's until the length of string is 2 chars.
echo str_pad($str - $int, 2, '0', STR_PAD_LEFT);
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
Please help me with the number format in php
for example
I have some calculations, like a+b=c I want answer as 999.99 format in php I've read the number_format,sprintf but its not useful. please give me some useful ideas. My final result should be in 999.99 format.
Per the 'not useful' documentation:
$num = 999.98353;
$num = number_format($num, 2, '.', '');
If you take the time to actually read the documentation, there are pertinent examples.
In addition to number_format, you can use sprintf
$a = 999;
$b = .99;
$c = $a + $b;
echo sprintf('%03.2f', $c); // 999.99
$num = 999.98353;
$num1 = 10;
$num2 = $num + $num1;
echo number_format($num2, 2, '.', ''); // output 1009.98
try this
$a = 500.3755;
$b = 600.9855;
$c = $a + $b;
echo number_format($c, 2, '.', ''); //1101.36
echo number_format($c, 2, '.', ','); //1,101.36
$a = 1234.5678;
$b = 1234.5678;
$num = round(($a + $b), 2);
echo number_format($num, 2, '.', ',');
number_format works, try posting some of your codes.
You can use
$c = sprintf('%0.2f',$a)+sprintf('%0.2f',$b);
Or you can also try :
$c = $a+ $b; // If $c = 999.9999
$c = substr($c, 0, 2); // $c is now the string 999.9999
echo( number_format($c-1,2) ); // Will output 998.99
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.