473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

formatting numbers with leading zeros

How can I format numbers with leading zeros? I believe I did this before
some time ago but I cannot remember how I did it.
I want the output be 100305 for 10:03:05.

string fname = string.Format("{0}{1}{2}.txt", dt.Hour, dt.Minute,
dt.Second);

I do not want to use ToString() because it generated to much temporary
objects, and DateTime.ToShortTimeString() is culture dependend and contains
colon characters.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jul 21 '05 #1
3 9649
use string.Format("{0:hh}{0:mm}{0:ss}.txt", dt);
or

use string.Format("{0:HH}{0:mm}{0:ss}.txt", dt);

/LM

"cody" <no****************@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
How can I format numbers with leading zeros? I believe I did this before
some time ago but I cannot remember how I did it.
I want the output be 100305 for 10:03:05.

string fname = string.Format("{0}{1}{2}.txt", dt.Hour, dt.Minute,
dt.Second);

I do not want to use ToString() because it generated to much temporary
objects, and DateTime.ToShortTimeString() is culture dependend and
contains
colon characters.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

Jul 21 '05 #2
Cody,
I would use:
string fname = string.Format("{0:HHmmss}.txt", dt);
NOTE: HH above formats hour as 00-23, hh formats hour as 01-12.

Alternatively you can use:
string fname = string.Format("{0:D2}{1:D2}{2:D2}.txt", dt.Hour, dt.Minute,
dt.Second);
For documentation on what is allowed & how to use String.Format see:

http://msdn.microsoft.com/library/de...ttingtypes.asp

Hope this helps
Jay

"cody" <no****************@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl... How can I format numbers with leading zeros? I believe I did this before
some time ago but I cannot remember how I did it.
I want the output be 100305 for 10:03:05.

string fname = string.Format("{0}{1}{2}.txt", dt.Hour, dt.Minute,
dt.Second);

I do not want to use ToString() because it generated to much temporary
objects, and DateTime.ToShortTimeString() is culture dependend and
contains
colon characters.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

Jul 21 '05 #3
> > string fname = string.Format("{0:HHmmss}.txt", dt);

NOTE: HH above formats hour as 00-23, hh formats hour as 01-12.

Alternatively you can use:
string fname = string.Format("{0:D2}{1:D2}{2:D2}.txt", dt.Hour, dt.Minute, dt.Second);

thanks thats exactly what I was looking for!

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
2
by: Stian | last post by:
Hi, I'm sort of new to Python and I've just discovered the amazing formatting using the % operator. One thing I can't figure out is how to format numbers so that they get leading zeros, for example...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
3
by: cody | last post by:
How can I format numbers with leading zeros? I believe I did this before some time ago but I cannot remember how I did it. I want the output be 100305 for 10:03:05. string fname =...
5
by: Bilgehan.Balban | last post by:
Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit...
2
by: chris | last post by:
Hi, I have a simple ms access application that allows you to scan barcodes in to a form which stores them in the database. The barcodes are 6 digits in length e.g. 555666 but my handheld...
5
by: Code::Tyr | last post by:
Hello. I have a simple problem and need help. I am developing a simple file system that stores info based on folder names. Now, I only have one problem. How do i format (int i = 2) into a string...
4
by: Vlinder | last post by:
I'm completely new to PHP (jumped ship this week from MSAccess and ASP to MySql and PHP) and I'd appreciate some help with leading zeros in telephone numbers. I've got a field with type...
2
by: lawpoop | last post by:
Hello all -- I'm trying to format decimal numbers for a report. I would have to have the number with leading zeros to the tens' place, and trailing zeros to the thousandths' place. I'm using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.