Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need 6 digits

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
I have a variable $strNum = "000000";
Throught the code output I need to have 6 digits for $strNum.

Here is the code
Code:
$strNum = '000000';
$intNum = '2';

while($strNum <= '1000000'){
   $strFinal = ($strNum * $intNum);
   $echo $strFinal;
   $strNum++;
}

The print out looks like this:
2
4
6
8
10
I want it to look like this
000002
000004
000006
000008
000010
and so on.
Any idea's?


Thanks
Timgerr

-How important does a person have to be before they are considered assissinated instead of just murdered?

 
If its only for display purposes, you can add the missing 0's by concatenating them to your original value like so:

Code:
if($strfinal<=9) {$strfinal="00000" . $strfinal2;}
if($strfinal>9) {$strfinal="0000" . $strfinal2;}
if($strfinal>99) {$strfinal="000" . $strfinal2;}
if($strfinal>999) {$strfinal="000" . $strfinal2;}
.
.
.

this will add the missing zeroes in fron of the number you are displaying. According to the number of digits already present in the number.

so if $strfinal was 25 it would display 000025

hope this helped
 
So there is no way of having 6 0's? I would think that there would be a function that would allow me to do this. I need to have all 6 0's for math functions and it would not be a good option to have to add all the 's in my functions.

-How important does a person have to be before they are considered assissinated instead of just murdered?

 
For math functions, "0000005" is exactly the same as "5", what kind of functions are you performing that would require 6 zeroes in front of an integer????
 
I need to be able to take a 6 digit number (000000) and multiply it by 2. Then take the output and see if in reverse it matches the first number. Here is an example 12345 x 2 = 54321. This example does not work but it is a chalange that I am working on.

-How important does a person have to be before they are considered assissinated instead of just murdered?

 
I see what you want to acomplish. And for this any number less than 6 digits in your case will never match another one in reverse becasue of the zeroes at the begining. So you could filter out anything less that 100,000. with an If statement. The rest of the numbers 100,000 to 999,999 would then be tested.
 
Did you take a look at sprintf() in the manual?

This code does what you want:
Code:
<?
$intNum = 2;

for ($strNum=1;$strNum<10;$strNum++){
   $strFinal = ($strNum * $intNum);
   printf ("%06d<br>", $strFinal);
}
?>

If you want to save the formated number instead of printing it, use $var=sprintf("%06d",$strFinal).

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top