Solved

php function to format number from 1 to 99 to 3 digits

Posted on 2008-10-15
5
811 Views
Last Modified: 2008-10-15
Take any number 1 to 100 and format so there are always 3 digits.

Example:

$numberA = 1;
$numberB = 10
$numberC = 85

Now format each as:

$numberA = 001;
$numberB = 010;
$numberC = 085;

So need function that is smart and can take any number 1-99 and format to 3 digits. So if number happens to 100 or 150 then dont touch number!

0
Comment
Question by:pdheady
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 300 total points
ID: 22726815

function myFormat($str){
	$str=(int)$str;
	if($str < 10 )
	{
		return "00".$str;
	}
	elseif($str < 100 )
	{
		return "0".$str;
	}
return $str;
}

Open in new window

0
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 100 total points
ID: 22726964
A slightly modified version:
<?php 
function myFormat($str){
  if(is_int($str) && $str < 100 )
  {
    $str=substr('00'.$str,-3);
  }
  return $str;
}
echo myFormat(1).'<br>';
echo myFormat(10).'<br>';
echo myFormat(100).'<br>';
echo myFormat(1000).'<br>';
echo myFormat("FOO").'<br>';
?>

Open in new window

0
 
LVL 11

Expert Comment

by:kelvinwkw
ID: 22727761
this will do

function format_number($number){
    return substr("000".$number, -3);
}


0
 
LVL 11

Expert Comment

by:kelvinwkw
ID: 22727768
oops sorry repost, please ignore my post
0
 
LVL 8

Assisted Solution

by:shanikawm
shanikawm earned 100 total points
ID: 22727847
use

sprintf("%03d",$number) or  printf("%03d",$number)

Attached code prints,

000
005
025
100
150
<?php
echo sprintf("%03d",0),"\n";
echo sprintf("%03d",5),"\n";
echo sprintf("%03d",25),"\n";
echo sprintf("%03d",100),"\n";
echo sprintf("%03d",150),"\n";
?>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question