This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I check if a number is a palindrome?
If an integer is to not allowed to be treated a string and typecasting is not allowed, how can we figure out whether the number is palindrome or not(in PHP)?
The program which I have come up with is:
function checkPalindrome($number){
$reverse_number = 0;
$number_backup = $number;
while($number > 0){
$reverse_number = $reverse_number * 10 + $number % 10;
$number /= 10;
}
return $reverse_number == $number_backup;
}
At step "$number/=10", the result generated won't be integer which is creating the problem.
If you are not allowed to typecast and string-handling is forbidden, you need to do some extra calculations:
while($number > 0){
$lsd = $number % 10;
$reverse_number = $reverse_number * 10 + $lsd;
$number = ($number - $lsd) / 10;
}
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Comparing floats - same number, but does not equal? [duplicate]
(3 answers)
Closed 2 years ago.
I want to check the given right hand side equation is equal to the left hand side or not. The value of $x = (19/30).
(((6*($x)) - 5)/(7 - (3*($x)))) = ((4 - (8 * $x))/((4 * $x) + 2))
I had tried below mentioned code, but it shown the RHS value is greater than LHS.
<?php
function check($m, $n)
{
$rhs = abs($m);
$lhs = abs($n);
if($rhs > $lhs){
echo "$m <strong style='color:red;'>is greater than</strong> $n";
}
elseif($rhs < $lhs) {
echo "$m <strong style='color:blue;'>is smaller than</strong> $n";
}
else{
echo "$m <strong style='color:green;'>is equal</strong> $n";
}
}
// $m = ((6*(19/30)) - 5)/(7 - (3*(19/30)));
// $n = (4 - (8 * (19/30)))/((4 * (19/30)) + 2);
$x = 19/30;
$m = abs(((6*($x)) - 5)/(7 - (3*($x))));
$n = abs((4 - (8 * $x))/((4 * $x) + 2));
check($m, $n);
?>
The output:
0.23529411764706 is greater than 0.23529411764706
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 5 years ago.
I'm trying to make a for loop with numbers given by user ($start and $limit), but I want to be able to write '0001' as $start and '1000' as $limit and print each one of numbers. The problem is, only the first number is printed as '000..' and after increment, those zeros disappear. This is my code:
$start = 0001;
$limit = 1000;
for ($i=$start; $i <= $limit; $i++) {
echo $i.'<br>';
}
outputs:
001
2
3
...
1000
Is there any way to make it as:
0001
0002
...
1000
Try
$start = 1;
$limit = 1000;
for ($i=$start; $i <= $limit; $i++) {
printf("%04d<br>",$i);
}
This question already has answers here:
Round to max thousand, hundred etc in PHP
(5 answers)
Closed 5 years ago.
PHP's floor() and ceil() is useful for rounding floats to their nearest integers.
But what if we need to find the highest multiple of 10 (hundreds, thousands, etc.) for a given number? For example:
num_to_scale(538.9) // 500
num_to_scale(543123654.01234) // 5000000
Try this code, check the live demo. You also can refer to this post.
$floor = floor($v);
return str_pad(substr($floor, 0, 1), strlen($floor), '0');
function num_to_scale($number) {
$multiple=1; $test=1;
for ($i=1; $test>=1; $i++) {
$factor = $multiple/10;
$test = $number/$multiple;
$multiple = $multiple*10;
$scale = floor($number/$factor)*$factor;
} return $scale;
}
or more simply:
function num_to_scale($n) {
$m=1; $t=1;
for ($i=1; $t>=1; $i++) {
$f=$m/10; $t=$n/$m; $m=$m*10; $s=floor($n/$f)*$f;
} return $s;
}
Combined with a helper function to convert integers to text strings, we can easily do something like this:
echo 'Over '.num_to_scale(5395206).' units sold.';
// Over five million units sold.
try this:
function num_to_scale($number) {
$num = (int) floor($number);
$ln = strlen($num) - 1;
return (int) floor($num / pow(10, $ln)) * pow(10, $ln);
}
My answer uses logarithm :
function num_to_scale($var) {
$nbDigits = (int)log($var,10);
return (int)($var/10**$nbDigits)*10**$nbDigits;
}
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
without use of round() function perfrom the round() in php
$a = "123.45785";
$v = round($a);
output: 123.46;
it had done by round function but i want to get output without use of round and number_format() function.
Here's a way of doing it with arithmetics:
function my_round($num, $places = 2) {
// Multiply to "move" decimals to the integer part
// (Save one extra digit for rounding)
$num *= pow(10, $places + 1);
// Truncate to remove decimal part
$num = (int) $num;
// Do rounding based on the last digit
$lastDigit = $num % 10;
if ($lastDigit >= 5)
$num += 10;
// Remove last digit
$num = (int) ($num/10);
// "Move" decimals in place, and you're done
$num /= pow(10, $places);
return $num;
}
You have sprintf.
$a = "123.45785";
echo sprintf("%01.2f", $a); // output: 123.46
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
php display number with ordinal suffix
I'm attempting to add ordinal contractions i.e.(st/nd/rd/th) to an increment.
Somehow I need to get the last digit of $i to test it against my if statements...
Here is my code so far:
$i = 1;
while($i < 101 ){
if($i == 1){$o_c = "st";}else{$o_c = "th";}
if($i == 2){$o_c = "nd";}
if($i == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
You can use the modulus (%) operator to get the remainder when dividing by 10.
$i = 1;
while($i < 101 ){
$remainder = $i % 10;
if($remainder == 1){$o_c = "st";}else{$o_c = "th";}
if($remainder == 2){$o_c = "nd";}
if($remainder == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
What about using the modulus operator: $i % 10?
Display numbers with ordinal suffix in PHP
(that thread has other solutions. I liked that one)