Generate the set of all X-digit numbers [duplicate] - php

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
Basically, I need to generate a list of all seven-digit numbers (for example, 1461979) from 0000000 to 9999999. I know there are 10^7 of them (ten million), but I can't think of an efficient function to output them. I'm sure there's a generic function out there that can do it - preferably in PHP, but I can port it if need be.

Loop from 0 to 9999999 and use one of the dozens of methods for zero-filling a number, such as printf("%07d", $val).

for ($i=0; $i<=9999999; $i++) {
echo sprintf("%07d", $i) . '<br / >';
}
This might break your browser though ;)

Related

Finding the Prime Number using a WHILE LOOP (not a for loop)in php [duplicate]

This question already has answers here:
A formula to find prime numbers in a loop
(22 answers)
Closed 6 years ago.
So I am just starting to learn a PHP in college. I have been given a simple task of finding the prime numbers of 101 in PHP but using a while loop. I cant figure it out.It ahs to use a while loop rather a for loop, teacher specifications. Many thanks.
This is my pathetic code,(& this only prints out the numbers from 1 to 101, im ever new to it)
<?php
$x = 1;
$maxNumber=101;
while($x <= $maxNumber) {
echo "The number is: $x <br>";
$x++;
}
?>
So, you would devide by 2. Check if the number turns into a decimal. If so it's prime.
You can use is_float. Also 2/2=1. Ignore 1.

php Rounding up numbers (3628 > 3630) [duplicate]

This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 6 years ago.
After converting currencies I end up with the numbers below:
3628, 5987 and 2359.
I'd like to round them up so they would appear as 3630, 5990 and 2360.
What is the best approach to accomplish this?
My idea of doing this is adding a 0. in front of the number to achieve 0.3628, then using round(0.3628, 3) so that I would get 0.363 and then finally id have to remove 0. and add another zero at the end to achieve 0.3630.
There must be a better way to do it.
Like so:
echo ceil(5987 / 10) * 10;
outputs 5990

How to append zero after 4.5 to make it 4.50 in PHP? [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 7 years ago.
I know it may be a silly one and it may be duplicate question as well, but I can't find any proper question posted before(may be its my incapability)
I have a value fetched from DB... its say like
4.5 which should be 4.500
0.01 which should be 0.010
11 which should be 11.000
How can I achieve this in PHP?
Simply format it with sprinft to make it a floating point number to 2 decimal places.
echo sprintf("%.2f", 4.5);
https://eval.in/378771
If you change the 2 to a 3 you will get your format you've listed.
echo sprintf("%.3f", 4.5) . PHP_EOL;
echo sprintf("%.3f", 0.01) . PHP_EOL;
echo sprintf("%.3f", 11) . PHP_EOL;
https://eval.in/378773
php.net/number_format
number_format(4.5,2);
Use the function number_format
Trailing zeros to the right of a decimal point is not supported in PHP. You need to format the number to string.
$value = number_format($value,3);
Modern way is to use NumberFormatter which formats numbers according to set locale.
$fmt = new NumberFormatter('en-US', NumberFormatter::DECIMAL);
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 3);
$fmt->format(1234567.891234567890000);
If You use mysql then Use FORMAT function
like SELECT FORMAT(4.5,4)
also str_pad(4.5,STR_PAD_RIGHT)
for more http://php.net/manual/zh/function.str-pad.php

Computing limits in PHP [duplicate]

This question already has answers here:
Is there something that's larger than any numbers in PHP?
(9 answers)
Closed 8 years ago.
I want to compute n^(2/3) as n approaches infinity in PHP.
What I tried:
$n = "INF";
echo pow($n, (2/3));
Desired result:
INF
Actual result:
0
Any suggestions? How to compute limits in PHP?
UPDATE:
OK, first problem solved - I just needed to remove the quotation mark.
But is this actually the way to calculate a limit? Is it interpreted as a limit?
You're passing in the string "INF" into the exponential expression, which isn't valid. Try passing in just POW:
$n = INF;
echo pow($n, (2/3));
// INF

convert number from 12 to 12.00 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In PHP, how to print a number with 2 decimals, but only if there are decimals already?
Brothers I want to convert me any way I entered into a text field automatically turns
12 to 12.00
How possible work by php and javascript
Thanks
In PHP its easy to do that by doing:
number_format($numberVar, 2);
Try this number_format() from PHP.net:
echo number_format(12, 2)
What, no javascript?
var x = 3;
alert(x.toFixed(2)); // 3.00
Will "work" for the case given, but so will:
alert(x + '.00');
There are bugs in toFixed in some older browsers with certain values so many write their own simple routine.
$thisnum = $thisnum . ".00";
Proof of concept:
$thisnum = $thisnum . ".00";
echo ($thisnum + 4.55);
However, as pointed out by the community, this is something that will only work for the case mentioned in the question and will not handle any use cases that already have decimals. The other answers are better.

Categories