|
|
|
|
![]() ![]() |
Jun 13 2006, 05:47 PM
Post
#1
|
|
|
Absolute Newbie Group: Admin Posts: 884 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
The Importance Of Using Leading Zeros Leading zeros are a very important tool for sorting and comparing data on computers. What is a leading zero? A leading zero does just what it sounds like, it leads the value. For example, lets convert a few numbers to leading zero numbers to make the new number 5 digits long. 234 => 00234 456 => 00456 6456 => 06456 12 => 00012 4 => 00004 677 => 00677 That's great right but why would you do that? This is done for 2 reasons actually. The first is to display numbers in a more uniform and easier to read format. Using the numbers from above, lets compare the format. Unformated: 234 456 6456 12 4 677 Formated: 00234 00456 06456 00012 00004 00677 The second and probably the most important reason we use leading zeros is to format our file names and other data which we want to have properly sorted. On some systems and some software, sorting filenames that contain numbers can cause undesired results. When we sort filenames alphabetically, we start on the left side and work our way to the right. This works fine and well but we sort numbers the opposite way. And when we sort an alphanumeric filename, we sort both ways, numbers sorted numerically and letters alphbetically. So using the numbers from above let's create a few filenames and sort them out the computer way and the correct way. System Sorting Method: filename12.txt filename234.txt filename4.txt filename456.txt filename6456.txt filename677.txt Correct Sorting Method: filename4.txt filename12.txt filename234.txt filename456.txt filename677.txt filename6456.txt This minor flaw in the default way that computers sort alphnumeric values can cause errors down the road when you try to sort data based on a flawed system. So either you can write a script to always sort your values using a better system which you'll have to define or you can simply format your values so that the system can sort them correctly by itself. This is where those leading zeros come in. The system wants to put "4" and "456" together because they both start with "4" kind of like putting "apple" and "album" together because they both start with "a". If we change "4" to "004", it still has the same numeric value but now the first character is zero and zero comes before the "1" in "12". So "004" comes before "12" or better yet, "012". So to sort our values correctly all of the time no mater haw the system wants to sorrt is, here is how we should format our values: filename0004.txt filename0012.txt filename0234.txt filename0456.txt filename0677.txt filename6456.txt Auto Generating Leading Zeros If you are using values that are generated by a script to make filenames with and you want to be sure that the filenames are sorted properly then you'll need a way of adding the leading zeros to your values. There are a few ways to attack the issue of adding leading zeros to your values when generating filenames in PHP. The first method is very straight forward and very limited in features. Here are a few lines of code that will work for values up to 999. CODE $value = 37; // This can be any number between 0 and 999 if ($value < 10){ // If the value is under 10 then 2 zeros are needed to make a 3 digit value. $new_value = "00" . $value; } else if ($value < 100){ // If the value is under 100 then 1 zero is needed to make a 3 digit value. $new_value = "0" . $value; } else{ // If the value is not less the 10 or 100 then is is already a 3 digit value. $new_value = $value; } echo $new_value; This would output "037" You can then add that to a filename prefix like "image" and a file extention like ".jpg"in this way: CODE $filename = "image" . $new_value . "jpg"; echo $filename; This would output "image037.jpg" Congratulations, you just generated your first leading zero alphanumeric filename. What if we knew that we were going to create a lot of filenames. Maybe we planned on creating 15,000 filenames or more. In that case we need to adjust our script to add more zeros but every time we add zeros, we have to check the next power of 10. CODE $value = 937; // This can be any number between 0 and 999 if ($value < 10){ // If the value is under 10 then 4 zeros are needed to make a 5 digit value. $new_value = "0000" . $value; } else if ($value < 100){ // If the value is under 100 then 3 zero is needed to make a 5 digit value. $new_value = "000" . $value; } else if ($value < 1000){ // If the value is under 1000 then 2 zero is needed to make a 5 digit value. $new_value = "00" . $value; } else if ($value < 10000){ // If the value is under 10000 then 1 zero is needed to make a 5 digit value. $new_value = "0" . $value; } else{ // If the value is 10000 or over then is is already a 5 digit value. $new_value = $value; } echo $new_value; This would output "00937" Ideally, we would create a function that would do this same thing and can be easily reused over and over like this: CODE function leading_zeros($value){ if ($value < 10){ // If the value is under 10 then 4 zeros are needed to make a 5 digit value. $new_value = "0000" . $value; } else if ($value < 100){ // If the value is under 100 then 3 zero is needed to make a 5 digit value. $new_value = "000" . $value; } else if ($value < 1000){ // If the value is under 1000 then 2 zero is needed to make a 5 digit value. $new_value = "00" . $value; } else if ($value < 10000){ // If the value is under 10000 then 1 zero is needed to make a 5 digit value. $new_value = "0" . $value; } else{ // If the value is 10000 or over then is is already a 5 digit value. $new_value = $value; } return $new_value; } Then we call the function like this: CODE echo leading_zeros(3157); This would output "03157" Now you can either place that function at the top of your scripts that you want to use it in or place the function in a file full of functions to include in your script. There is one problem with our new function. It only works for 5 digit values. We may not always want a 5 digit value. Maybe we plan to have 100 icon images and 100,000 background images. We need a better function. In order to get the required number of zeros we need to use powers of 10: 10^1 = 10 10^2 = 100 10^3 = 1,000 10^4 = 10,000 10^5 = 100,000 10^6 = 1,000,000 etc... So to generate a 6 digit value with leading zeros, would express it like so: CODE function leading_zeros($value){ if ($value < pow(10, 1)){ $new_value = "00000" . $value; } else if ($value < pow(10, 2)){ $new_value = "0000" . $value; } else if ($value < pow(10, 3)){ $new_value = "000" . $value; } else if ($value < pow(10, 4)){ $new_value = "00" . $value; } else if ($value < pow(10, 5)){ $new_value = "0" . $value; } else{ $new_value = $value; } return $new_value; } Now we can see the following pattern for a 6 digit value: 10 to the power of 1 needs 5 zeros 10 to the power of 2 needs 4 zeros 10 to the power of 3 needs 3 zeros 10 to the power of 4 needs 2 zeros 10 to the power of 5 needs 1 zeros From that pattern we can see another patern: 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6 4 + 2 = 6 5 + 1 = 6 So the number of zeros equals the desired number of digits minus the current power of 10. CODE $zeros = $digits - $x; Now with a couple of loops and a bunch of variables, we can write a new function which allows us to generate values with leading zeros no matter how many digits we desire. CODE function leading_zeros($value, $digits){ for($x = 1; $x <= $digits; $x++){ $ceiling = pow(10, $x); if($value < $ceiling){ $zeros = $digits- $x; for($y = 1; $y <= $zeros; $y++){ $leading .= "0"; } $x = $digits+ 1; } } $output = $leading . $value; return $output; } To call this function now, we use a 2 argument call like this: CODE echo leading_zeros(5195, 7); This would output "0005195" CODE echo leading_zeros(537, 5); This would output "00537" CODE echo leading_zeros(12, 3); This would output "012" The arguments for the function are the value that you want to convert to leading zeros and then the number of total digits use for the new value. So leading_zeros(12, 3) means create a 3 digit leading zero number out of the number 12. Now lets try to walk through the function and explain it. CODE function leading_zeros($value, $digits){ for($x = 1; $x <= $digits; $x++){ $ceiling = pow(10, $x); if($value < $ceiling){ $zeros = $digits- $x; for($y = 1; $y <= $zeros; $y++){ $leading .= "0"; } $x = $digits+ 1; } } $output = $leading . $value; return $output; }
I hope everyone find this helpful. vujsa |
|
|
|
Oct 5 2007, 10:41 PM
Post
#2
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
Don't forget Connecticut has a zip code beginning with zero! So, this is important for varchar storage in DB's and then the application truncating the leading zeros, hehe. BUG
|
|
|
|
Dec 17 2007, 01:40 AM
Post
#3
|
|
|
BUG.SWAT.PATROL Group: Members Posts: 626 Joined: 1-September 04 From: Auckland, New Zealand Member No.: 27 |
Sort of re-inventing the wheel Vujsa,
There's str_pad in PHP and my favorite sprintf() for formatting strings (learnt this in C actually, but glad to see it in PHP) to have leading zeros or leading any character really. These two methods are a lot more elegant in my views, haven't actually looked at this completely, but I think that was the main gist you were trying to accomplish. Cheers, MC |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 21st August 2008 - 11:11 PM |