Comment On IFormatProvider? Oh no, not me ...

One of the cooler features of higher level languages is the built-in support for the ever-so-common task of formatting numbers. The .NET Framework provides the rather ubiquitous String.Format() method to help out with formatting numbers, dates, and everything else. VB.NET makes it even easier, carrying over the Format() method from previous versions of visual basic. That said, Ant Day was a bit surprised to a plethora of examples like this in a VB.NET solution developed from a highly-paid consulting company that "adapts leading-edge technology, best practices, and experience to help customers realise their business objectives:" [expand full text]
« PrevPage 1 | Page 2Next »

Re: IFormatProvider? Oh no, not me ...

2005-04-19 14:22 • by dubwai

The funniest thing about this to me is that not only does it reinvent the wheel, it reinvents it as an oval.


I've had to write code to do this in a language that didn't have this feaure (or at least I didn't know what it was) and I would just add "000000" to the front and then substring the last 6 characters.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 14:25 • by yawmark
Cripes.



// not far from...

public int intValue(int i) {

    return i;

}

Re: IFormatProvider? Oh no, not me ...

2005-04-19 14:29 • by stevekj
Three words:  W T F???



In Perl (non-production code!) I've used inelegant formatting constructs like this:



  index="000000$i"

  rtindex=$((${#index}-5))

  nrtindex=${index:$rtindex}


But switch/case?  Ack!



(Note I am not a particularly expert Perl programmer, and there's almost certainly a more elegant way to do that.)



Re: IFormatProvider? Oh no, not me ...

2005-04-19 14:35 • by loneprogrammer
Why not use an XSLT stylesheet to format this output?

Re: IFormatProvider? Oh no, not me ...

2005-04-19 14:49 • by Charles Nadolski
32965 in reply to 32961
dubwai:

The funniest thing about this to me is that not only does it reinvent the wheel, it reinvents it as an oval.


I've had to write code to do this in a language that didn't have
this feaure (or at least I didn't know what it was) and I would just
add "000000" to the front and then substring the last 6 characters.





You could also do...



while(strWhatever.GetLength()<6)

    strWhatever.Insert('0');



Though your version might use fewer CPU cycles :)

Re: IFormatProvider? Oh no, not me ...

2005-04-19 14:49 • by John Bigboote
Alex Papadimoulis:
...a highly-paid consulting company that "adapts leading-edge technology..."





Well, they sure did that. Adapted the hell out of it, I would say.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:09 • by Irrelevant
32969 in reply to 32963
stevekj:


...

In Perl (non-production code!) I've used inelegant formatting constructs like this:



  index="000000$i"

  rtindex=$((${#index}-5))

  nrtindex=${index:$rtindex}




erm... that's not perl. You've got $s missing off the variables to the
left of the equalss, no semicolons, $(...) is invalid, and I'm not even
going to try to guess what ${index:$rtindex} is supposed to do.



Not trying to troll, that's just too alarmingly non-perl to not comment. But in terms of elegant solutions,

$str = sprintf '%05d', $i;

and

$str = '0' x (5 - length "$i") . $i;

both spring to my mind. And (take note all perl-is-ugly fundies)
neithier of these are particularly ugly (unlike stevekj's non-perl
there ;] ).

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:13 • by noone

So the first one would be from CSC Australia, then?


http://au.country.csc.com/en/mcs/mcs43/index.shtml contains the incriminating quote, though thankfully no actual code.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:13 • by John Bigboote
32971 in reply to 32969
Irrelevant:




erm... that's not perl.





I think it's P#.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:18 • by Rick Mogstad
inputEmployeeNo.PadLeft(6, "0"c)   seems the easiest way to do it to me...





Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:19 • by Matt
32973 in reply to 32964
loneprogrammer:
Why not use an XSLT stylesheet to format this output?




uh, because like the original post says, the .NET Framework has extensive built-in support for number formatting?

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:22 • by RyGuy

adapts leading-edge technology, best practices, and experience to help customers realise their business objectives


Looks awfully lot like what can be found on this page...


http://au.country.csc.com/en/mcs/mcs43/index.shtml

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:22 • by Charles Nadolski
32975 in reply to 32972
Anonymous:
inputEmployeeNo.PadLeft(6, "0"c)   seems the easiest way to do it to me...








Any VB.NET people know if you can use that method?  If so, that's a one-line replacement for WTF #1.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 15:28 • by John Bigboote
32976 in reply to 32975
Charles Nadolski:
Anonymous:
inputEmployeeNo.PadLeft(6, "0"c)   seems the easiest way to do it to me...








Any VB.NET people know if you can use that method?  If so, that's a one-line replacement for WTF #1.




I would just leave the emp no as an int value and do




intValue.ToString("000000")



It may be that the conversion to string was only done for the length-check/padding anyway.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 16:12 • by dopefish
32978 in reply to 32969
Irrelevant:
stevekj:


...

In Perl (non-production code!) I've used inelegant formatting constructs like this:



  index="000000$i"

  rtindex=$((${#index}-5))

  nrtindex=${index:$rtindex}




erm... that's not perl. You've got $s missing off the variables to the
left of the equalss, no semicolons, $(...) is invalid, and I'm not even
going to try to guess what ${index:$rtindex} is supposed to do.



Not trying to troll, that's just too alarmingly non-perl to not comment. But in terms of elegant solutions,

$str = sprintf '%05d', $i;

and

$str = '0' x (5 - length "$i") . $i;

both spring to my mind. And (take note all perl-is-ugly fundies)
neithier of these are particularly ugly (unlike stevekj's non-perl
there ;] ).




It's actually bash (UNIX shell), not Perl.  And it does work,
though it's still not a particularly good solution (bash has a printf
command just like the C function, you know...)

Re: IFormatProvider? Oh no, not me ...

2005-04-19 16:14 • by cm5400
[:S]  Wow.  That was depressing.  WTF!

Re: IFormatProvider? Oh no, not me ...

2005-04-19 17:19 • by nonDev
32982 in reply to 32962

yawmark:
Cripes.
There are 3 kinds of people in the world. Those who can count, and those who can't.


Actually there are 10 kinds of people in the world. Those who count binary and thouse who don't.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 17:33 • by diaphanein
32985 in reply to 32982
nonDev:

yawmark:
Cripes.
There are 3 kinds of people in the world. Those who can count, and those who can't.


Actually there are 10 kinds of people in the world. Those who count binary and thouse who don't.



But if only you and dead people can read hex, how many people can read hex? [:D]

Re: IFormatProvider? Oh no, not me ...

2005-04-19 17:35 • by dubwai
32986 in reply to 32965

Charles Nadolski:


You could also do...

while(strWhatever.GetLength()<6)
    strWhatever.Insert('0');

Though your version might use fewer CPU cycles :)


Not sure about CPU cycles but if String is immutable in the language in question, this creates a bunch of junk Objects.  Never more than 6 but still.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 17:52 • by Cthulhon
32987 in reply to 32985
Anonymous:
nonDev:

yawmark:
Cripes.
There are 3 kinds of people in the world. Those who can count, and those who can't.


Actually there are 10 kinds of people in the world. Those who count binary and thouse who don't.



But if only you and dead people can read hex, how many people can read hex? [:D]



Well, that's 1 (you) plus DEAD which is 57005 is decimal, so one can reasonably assume that about 57006 can read hex.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 18:14 • by MeMe
32988 in reply to 32985
Anonymous:
nonDev:

yawmark:
Cripes.
There are 3 kinds of people in the world. Those who can count, and those who can't.


Actually there are 10 kinds of people in the world. Those who count binary and thouse who don't.



But if only you and dead people can read hex, how many people can read hex? [:D]





A better question would be...





If only you, me and dead people can read hex, how many people can read hex?

Re: IFormatProvider? Oh no, not me ...

2005-04-19 20:06 • by Free
32990 in reply to 32982

Those who understand binary and those who don't. ( 10 - 3  == 1 )


 


There are 11 kinds of people, those who understand string theory and those who don't.


 

Re: IFormatProvider? Oh no, not me ...

2005-04-19 20:28 • by bat
This is the line I really like:




Select Case (inputEmployeeNo.Length) ' Evaluate userid.




In what way is "inputEmployeeNo.Length" evaluating a userid (whatever
that may be)?  It's evaluating the length of the ID in digits, not
the ID itself.  Insane.




Incidentally: the Perl idiom I would use is sprintf("%04d",$n)
rather than any other silliness.  Why?  Because it's good
enough, it's fast enough, and it's obvious to anyone who's familiar
with the C [sf]printf format, which is a large majority of programmers.



Oh, and regarding the kinds of people: my favourite version is:




There are two kinds of people in the world: those who divide the people in the world into two kinds, and those who don't.




But the original poster who brought it up was misquoting a subtly different (and considerably wittier) original:




There are three kinds of people in the world: those who count, and those who don't.




No, it's not a typo; it's wit.

Re: IFormatProvider? Oh no, not me ...

2005-04-19 21:07 • by Dave Markle
Just in case you were wondering, Example 2 is C#.  *sob*

Re: IFormatProvider? Oh no, not me ...

2005-04-19 22:26 • by bubezleeb
32993 in reply to 32974
Check out this page http://au.country.csc.com/en/mcs/mcs46/index.shtml
in Firefox (and perhaps other Non-IE browsers) to see how much on the
leading edge they are!

Re: IFormatProvider? Oh no, not me ...

2005-04-20 00:25 • by Jon Limjap
Read The F_ing Manual!

Read The F_ing MSDN!

Read the F_ing Intellisense!



WTF, just read!!!

Re: IFormatProvider? Oh no, not me ...

2005-04-20 01:27 • by Drak
32995 in reply to 32994

At least in the second example you get an empty string if you input the wrong month. Reminds me of something with 13 months in a fiscal year? Month 13? Oh, thats "" [:O]


Drak

Re: IFormatProvider? Oh no, not me ...

2005-04-20 03:33 • by midas_dk
32996 in reply to 32972

Anonymous:
inputEmployeeNo.PadLeft(6, "0"c)   seems the easiest way to do it to me...



http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclasspadlefttopic.asp

Re: IFormatProvider? Oh no, not me ...

2005-04-20 03:41 • by Mario
Sigh:
strEmployeeNumber = Right("000000" & inputEmployeeNo, 6)
Or: -for those that really want an "if" statement:
if len(cstr(l_intMonthNum))> 1 then

return cstr(l_intMonthNum)
else
return cstr("0" & l_intMonthNum)
end if

Re: IFormatProvider? Oh no, not me ...

2005-04-20 04:07 • by fatgeekuk

In the mid to late 80s I worked as an analyst programmer at a company developing applications for the shipping industry, specifically to be used on board ships.


One of the other developers was clue impaired and routinely did...


 


if this = that then do something else i = i


 


he insisted that strange things happened if he excluded an else clause and this was his way of doing a no-op.


The same guy wrote a screen clearing routine like this (it was basic)


print


print


print


... (20 more) ...


print


when asked about loop optimisation and if that was what he was doing, he didnt know what I was talking about.


 


so, you see, some things don't change.

Re: IFormatProvider? Oh no, not me ...

2005-04-20 04:11 • by fatgeekuk
In that month number format, forgetting for the moment the general stupidity, would it not be better to throw an exception on the default case?

Re: IFormatProvider? Oh no, not me ...

2005-04-20 08:40 • by Mike R
33003 in reply to 32998

"when asked about loop optimisation and if that was what he was doing, he didnt know what I was talking about."


Loop optimisation, hell. What about CLS?

Re: IFormatProvider? Oh no, not me ...

2005-04-20 09:50 • by fatgeekuk
33004 in reply to 33003

Well yes, but we where developing on both


 


HP-150b (using MS CBASIC on MSDOS) and IBM PCs, the HPs did not support things like CLS as the


system was a NONIBMPC MSDOS system that was effectively a graphic terminal with a PC shoved in the bottom of the case, so the interface between the "PC" and the terminal was done by printing sets of ANSI command strings. :-(


But, even so, there WAS an ANSI sequence for clear screen. so, no excuses I spose.

Re: IFormatProvider? Oh no, not me ...

2005-04-20 09:52 • by Drak
33005 in reply to 33004

10 print "{insert inverted heart symbol here}";


Usually the first line of any program I ever wrote on my VIC 20


Drak

Re: IFormatProvider? Oh no, not me ...

2005-04-20 09:53 • by Zatanix
33006 in reply to 33003
Or perhaps more normal:     PRINT"§"      , where § is a reversed heart

Re: IFormatProvider? Oh no, not me ...

2005-04-20 09:53 • by Zatanix
33007 in reply to 33006
argh, drak beat me to it! :)

Re: IFormatProvider? Oh no, not me ...

2005-04-20 10:26 • by Xepol

For when you absolutely have to have your data your way!

Re: IFormatProvider? Oh no, not me ...

2005-04-20 10:28 • by JamesCurran
33010 in reply to 32998
fatgeekuk:
if this = that then do something else i = i

he insisted that strange things happened if he excluded an else clause and this was his way of doing a no-op.


I imagine he once got caught by the construct:


IF A=B THEN
        IF C=D THEN
              do something
ELSE
        IF E=D THEN
              do something else


 


 


 


 

Re: IFormatProvider? Oh no, not me ...

2005-04-20 10:33 • by Charles Nadolski
33012 in reply to 32993
Anonymous:
Check out this page http://au.country.csc.com/en/mcs/mcs46/index.shtml
in Firefox (and perhaps other Non-IE browsers) to see how much on the
leading edge they are!




Heh, I didn't notice it at first, for those wondering what the WTF is, just scroll down the page.

Re: IFormatProvider? Oh no, not me ...

2005-04-20 12:11 • by Miles Archer
How about a bogosort version? That would be cool.

Re: IFormatProvider? Oh no, not me ...

2005-04-20 12:35 • by Charles Nadolski
33021 in reply to 33020
Anonymous:
How about a bogosort version? That would be cool.




Hire a bunch of monkeys to smash the keyboard until the proper number of zeroes are padded to the string?

This looks like a job for Primate Programming Inc.

Re: IFormatProvider? Oh no, not me ...

2005-04-20 15:51 • by wakeskate
33040 in reply to 32994
Jon Limjap:
Read The F_ing Manual!

Read The F_ing MSDN!

Read the F_ing Intellisense!



WTF, just read!!!




I made a comment a couple days back that we shouldn't blame noobs for
not knowing the framework, but are there still people who program
without google groups or msdn in .NET?  I can't possibly disagree with that quote...





About that website though - any company with 4,000 employees is going to have
tonnes of horrible programmers (let's include whoever made that
website).  Most of the ones that have been procedural programming for
the last 10 years and just switching into oo now are too stubborn to
learn anything new properly.  Before you slag me for that you should
know I worked for a company with 46,000 IT staff and half the
programmers did daily WTF's - mostly VB6 and cobol programmers stepping
over to .NET.

Re: IFormatProvider? Oh no, not me ...

2005-04-20 17:32 • by sas
I know where you can find a stupider solution.  On our HR website,
you login with your employee id.  They just have a blurb that
says: ":"



If you guess wrong on the number of digits they're thinking about, you get this: "Employee
ID# not found. Please read directions carefully - check your Employee
ID# entry; be sure to enter a total of 6 digits, using zero(s) as
needed."



Why code it, when you can just force the user to fix it?


Re: IFormatProvider? Oh no, not me ...

2005-04-20 19:05 • by Maurits
33055 in reply to 33050
sas:
be sure to enter a total of 6 digits, using zero(s) as
needed




On your last day, hack in to the database and set the identity seed to 999999 - they won't be able to hire anyone else ;)

Re: IFormatProvider? Oh no, not me ...

2005-04-21 11:52 • by ammoQ
In terms of speed and memory consumption, both WTFs are probably better than the usual straight-forward solution.



The first example does the most simple possible action - append the string to a constant length string.

Probably there will be only one intermediate object, a StringBuilder used by the compiler, which cannot be avoided.



The second example always returns a constant string, so it doesn't even have to create a new object.



This are examples where ugly WTF-solutions probably perform better than
anything else. Although I doubt anyone will ever notice the difference

Re: IFormatProvider? Oh no, not me ...

2005-04-21 12:46 • by Rick Mogstad
33103 in reply to 32975
Charles Nadolski:
Anonymous:
inputEmployeeNo.PadLeft(6, "0"c)   seems the easiest way to do it to me...








Any VB.NET people know if you can use that method?  If so, that's a one-line replacement for WTF #1.




Yes, this works in VB.NET as well as C# I would imagine.  Its a
method in the string class, so it should work in any managed language.



The real syntax would of course be



inputEmployeeNo = inputEmployeeNo.PadLeft(6, "0"c)

Re: IFormatProvider? Oh no, not me ...

2005-04-23 22:41 • by AdmittedHack
33241 in reply to 33012
They have their site map / site outline at the bottom of every page ..  WTF ???

Re: IFormatProvider? Oh no, not me ...

2005-04-27 09:09 • by joss
33376 in reply to 33097
Exactly. The second example is good code IMHO, its totally clear how it
behaves, its not bug prone, its a few lines longer than obvious
alternative, but so what, its much faster than the alternatives and
although this wouldnt make a difference unless it was called *very*
frequently, why the hell not just write a fast function in the first
place.

Re: IFormatProvider? Oh no, not me ...

2005-04-27 11:23 • by wtijsma
33386 in reply to 32987

Anonymous:

Well, that's 1 (you) plus DEAD which is 57005 is decimal, so one can reasonably assume that about 57006 can read hex.


I can read some hex too, that would mean deaf people can read hex

Re: IFormatProvider? Oh no, not me ...

2005-04-27 17:21 • by Scott Vachalek
Well assuming they had the profiler numbers to back it up, there is
probably a performance advantage here over the default implementations,
especially the latter case.  Particularly against the Java
alternatives, which probably carried over to C#--they are probably an
order of magnitude or two slower. 



An array lookup would have been cleaner still, and we can probably
assume there was no such excuse, but heck I just have to play devil's
advocate sometimes...

« PrevPage 1 | Page 2Next »

Add Comment