Jump to content

add leading zeros


jeff5656

Recommended Posts

How do I add leading zeros if the variable does not contain them?

 

I have a list of numbers stored in a VARCHAR field. 

I want the number to always have 3 places, so if a number is 1.25 I want to change it to 001.25

if the number is 12.5 I want to change it to 012.5.  If the number is 123.5 I want to leave it alone.

 

I want to put all the records in a while loop and then check each variable and then update that record if it needs it.

 

What is the best way to do this?  Thanks!

Link to comment
Share on other sites

Ok I fiugured out how to have leading zeros, but then it cuts off the zeros to the right of the decimal point.

 

if I have 1.1 I want it to become 001.100

 

so:

$x = 1.1
$y = sprintf("%03d",$x);
echo $y

answer: 001

 

if I do this:

$x = 1.1
$y = sprintf("%.3f",$x);
echo $y

 

I get 1.100

 

How do I combine this so that I get the leading and the following zeros, i.e. 001.100?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.