473,386 Members | 1,720 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,386 software developers and data experts.

formatting strategies

Requirements for a field that represents an EmployeeNumber:

· The field is always 6 characters long, and is stored as a 'string'
in the db

o All characters must be digits

§ Regex pattern?

o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').

§ String.Format({"0:D6", empNumber) ?

o The non-padded number should be easily extractable

§ empNbrString.Substring(_firstCharGreaterThanZero(e mpNbrString))
(ie, '006728' == 6728, '000012' == 12)

Each requirement sounds relatively easy to do by brute force, but I'm sure
there is a more elegant approach some of you experts have used before.

Would you use a custom format provider? That may be overkill for the
employee, but I have similar formatting requirements for projectNumbers
which are complicated by the fact that different formats may apply to the
same project, depending on the organizational point of view.

Thanks for the Guidance
Aug 16 '08 #1
5 1219
Berryl Hesh <ef******@yahoo.comwrote:
Requirements for a field that represents an EmployeeNumber:

· The field is always 6 characters long, and is stored as a 'string'
in the db
Why is it stored as a string in the database if it's always an integer?
o All characters must be digits

§ Regex pattern?
That would certainly be pretty simple. Or just check the length and
that each character is in the range 0-9, which is easy by brute force.
It depends on how regex-savvy the maintainers will be.
o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').
I thought you said it *was* always 6 characters?
§ String.Format({"0:D6", empNumber) ?
That will only work if empNumber is an integer rather than a string.
o The non-padded number should be easily extractable

§ empNbrString.Substring(_firstCharGreaterThanZero(e mpNbrString))
(ie, '006728' == 6728, '000012' == 12)
Just use int.Parse.
Each requirement sounds relatively easy to do by brute force, but I'm sure
there is a more elegant approach some of you experts have used before.

Would you use a custom format provider? That may be overkill for the
employee, but I have similar formatting requirements for projectNumbers
which are complicated by the fact that different formats may apply to the
same project, depending on the organizational point of view.
I don't see any particular call for a custom format provider here, but
it depends on what you want to do. For data binding that *may* be
useful - I'm not terribly hot on data binding.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #2

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Berryl Hesh <ef******@yahoo.comwrote:
Requirements for a field that represents an EmployeeNumber:

· The field is always 6 characters long, and is stored as a
'string'
in the db
>>Why is it stored as a string in the database if it's always an integer?
Good point. I was guessing that PSoft displayed it that way because it
stored it that way. Truthfully I have no idea why they even display it this
way, but they've been fairly successful so there must be something behind
that.
o All characters must be digits

§ Regex pattern?
>>>That would certainly be pretty simple. Or just check the length and
that each character is in the range 0-9, which is easy by brute force.
It depends on how regex-savvy the maintainers will be.
I'm not very savvy with that myself - could you show me what the Regex check
would look like??
o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').
>>I thought you said it *was* always 6 characters?
As a convenience to data input.
§ String.Format({"0:D6", empNumber) ?
That will only work if empNumber is an integer rather than a string.
o The non-padded number should be easily extractable

§ empNbrString.Substring(_firstCharGreaterThanZero(e mpNbrString))
(ie, '006728' == 6728, '000012' == 12)
>>>Just use int.Parse.
Yup - Thank you
Each requirement sounds relatively easy to do by brute force, but I'm sure
there is a more elegant approach some of you experts have used before.

Would you use a custom format provider? That may be overkill for the
employee, but I have similar formatting requirements for projectNumbers
which are complicated by the fact that different formats may apply to the
same project, depending on the organizational point of view.
>>I don't see any particular call for a custom format provider here, but
it depends on what you want to do. For data binding that *may* be
useful - I'm not terribly hot on data binding.
I was thinking about a different formatting scenario with project numbers
that may be formatted differently for different systems. The number is
always based on the year the project is assigned and the next available
number. A legacy accounting system, for example, requires that number to
look like 008-00132 (assuming 132 is the next number available for a project
assigned in 2008). Project managers just use 08-132 because its easier to
work with.

Thanks for the response.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #3
Berryl Hesh <ef******@yahoo.comwrote:
>Why is it stored as a string in the database if it's always an integer?
Good point. I was guessing that PSoft displayed it that way because it
stored it that way. Truthfully I have no idea why they even display it this
way, but they've been fairly successful so there must be something behind
that.
So that's a "given" - you don't have control over it?
>>That would certainly be pretty simple. Or just check the length and
that each character is in the range 0-9, which is easy by brute force.
It depends on how regex-savvy the maintainers will be.
I'm not very savvy with that myself - could you show me what the Regex check
would look like??
Here's some code which uses the pattern and tests it:

using System;
using System.Text.RegularExpressions;

class EmployeeNumber
{
static readonly Regex Pattern = new Regex("^[0-9]{6}$",
RegexOptions.Compiled);

static void Main()
{
Test("123456");
Test("1234567");
Test("12345");
Test("12AA56");
Test("AA123456BB");
}
static void Test(string candidate)
{
Console.WriteLine("{0}: {1}",
candidate,
Pattern.IsMatch(candidate));
}
}

o If the number of digits in the EmployeeNumber is less than 6
characters, pad the number with leading 0's (i.e., 12 == '000012', 6728 ==
'0006728').
>I thought you said it *was* always 6 characters?
As a convenience to data input.
Okay, I see what you mean. In that case I'd parse it to an integer,
then pad it as you showed below.
>I don't see any particular call for a custom format provider here, but
it depends on what you want to do. For data binding that *may* be
useful - I'm not terribly hot on data binding.
I was thinking about a different formatting scenario with project numbers
that may be formatted differently for different systems. The number is
always based on the year the project is assigned and the next available
number. A legacy accounting system, for example, requires that number to
look like 008-00132 (assuming 132 is the next number available for a project
assigned in 2008). Project managers just use 08-132 because its easier to
work with.
I think you can still do that reasonably easily with just a custom
format string (but not a custom provider). Not sure how feasible the
parsing would be, but the formatting would be easy.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 16 '08 #4
Thanks for the info!

RE: data binding.
I've seen another experienced programmer that I respected also knock data
binding, awhile ago (I think it was Jeremy Miller), yet I see other
programmers, not to mention MS, continue to spend what seems like
considerable effort on it - do you have any links on the topic you
particularly like?

- Berryl Hesh
Aug 17 '08 #5
Berryl Hesh <ef******@yahoo.comwrote:
RE: data binding.
I've seen another experienced programmer that I respected also knock data
binding, awhile ago (I think it was Jeremy Miller), yet I see other
programmers, not to mention MS, continue to spend what seems like
considerable effort on it - do you have any links on the topic you
particularly like?
It's not that I particularly dislike it - although I have had problems
on the rare occasions when I've used it - but I just don't do much
client side coding, so I haven't had much experience with it.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 17 '08 #6

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

Similar topics

2
by: robin | last post by:
The Oblique Strategies were originally a set of one-hundred cards, each bearing a short phrase. They were devised by Brian Eno and Peter Schmidt as ways of working through creative problems. When a...
13
by: CoreyWhite | last post by:
When playing games, perhaps the most simple is tic-tac-toe. The game has two simple strategies, one is defensive and the other offensive. It is not hard at first to learn how to tie games when...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
0
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...

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.