![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi there, I am wondering if there is a function doing the same as in PHP strpad()? What I want to do is to make sure a string is always 2 characters long. If it is one character the function should add a 0 in front. Eg: make a "04" out of "4" Is this possible? Thank you for any hint, Merlin |
#3
| |||
| |||
|
|
Merlin wrote: Hi there, I am wondering if there is a function doing the same as in PHP strpad()? What I want to do is to make sure a string is always 2 characters long. If it is one character the function should add a 0 in front. Eg: make a "04" out of "4" Is this possible? Thank you for any hint, Merlin function strpad(val){ return (!isNaN(val) && val.toString().length==1)?"0"+val:val; } Or as a method of the string: String.prototype.strpad=function(){ return (!isNaN(this) && this.toString().length==1)?"0"+this:this; } Mick |
#4
| |||
| |||
|
|
Merlin wrote: Hi there, I am wondering if there is a function doing the same as in PHP strpad()? What I want to do is to make sure a string is always 2 characters long. If it is one character the function should add a 0 in front. Eg: make a "04" out of "4" Is this possible? Thank you for any hint, Merlin function strpad(val){ return (!isNaN(val) && val.toString().length==1)?"0"+val:val; } Or as a method of the string: String.prototype.strpad=function(){ return (!isNaN(this) && this.toString().length==1)?"0"+this:this; } |
#5
| ||||
| ||||
|
|
JRS: In article <BGBGc.16193$bp1.4228 (AT) twister (DOT) nyroc.rr.com>, seen in news:comp.lang.javascript, Mick White <mwhite13 (AT) BOGUSrochester (DOT) rr.com posted at Tue, 6 Jul 2004 17:53:37 : function strpad(val){ return (!isNaN(val) && val.toString().length==1)?"0"+val:val; } Or as a method of the string: String.prototype.strpad=function(){ return (!isNaN(this) && this.toString().length==1)?"0"+this:this; } Published code should ALWAYS be indented according to structure. |
|
I see no need to check with isNaN; if someone wants to add a leading zero, (a) it is permissible to do this to entities which are not decimal numbers (e.g. to Hex number strings), (b) if the entity is not like a number, there's probably a bigger mistake somewhere, and an error here could be helpful. |
| For converting a Number to a string of at least two digits representing the value, function LZ(x) { return (x<0||x>=10?"":"0") + x } Above, function strpad, if given a Number, returns either a Number or a String. This could cause later confusion. If a zero is added, there are two conversions of val to String. |
|
These might be better; String is probably cheap when applied to a string :- function lz(s) { var t = String(s) return t.length==1 ? "0"+t : t } // or <2 ?? function lz(s) { var t return (t = String(s)).length==1 ? "0"+t : t } // ?? |
#6
| |||
| |||
|
|
Dr John Stockton wrote: Published code should ALWAYS be indented according to structure. This is surely a matter of personal preference, flush left presentation doesn't suffer as much from the display quirks of many media as do indented, tabbed, "n" spaced or <pre> markup. IMO. |
#7
| |||
| |||
|
|
Mick White said: Dr John Stockton wrote: Published code should ALWAYS be indented according to structure. This is surely a matter of personal preference, flush left presentation doesn't suffer as much from the display quirks of many media as do indented, tabbed, "n" spaced or <pre> markup. IMO. None of the display quirks that I've ever seen make code as unreadable as flush left, IMO. |
#8
| |||
| |||
|
|
Lee wrote: Mick White said: Dr John Stockton wrote: Published code should ALWAYS be indented according to structure. This is surely a matter of personal preference, flush left presentation doesn't suffer as much from the display quirks of many media as do indented, tabbed, "n" spaced or <pre> markup. IMO. None of the display quirks that I've ever seen make code as unreadable as flush left, IMO. You're not from a background in C++ are you? But both you and I agree that it's a matter of opinion.... I have no problem with those who format their code with tabs or two-by-fours or whatever, but the code that I write is, for the most part, for my own consumption. There is a place for AR folks in programming, organization is very important. |
|
Important to me: Meaningful variable names.(Thanks to Dr. S for the prod) if(Boolean){code;} Always use braces after conditional Break out of loop/function asap. Efficiency of the code (lot to learn here) |
#9
| ||||
| ||||
|
|
Dr John Stockton wrote: Published code should ALWAYS be indented according to structure. This is surely a matter of personal preference, flush left presentation doesn't suffer as much from the display quirks of many media as do indented, tabbed, "n" spaced or <pre> markup. IMO. |
|
For converting a Number to a string of at least two digits representing the value, function LZ(x) { return (x<0||x>=10?"":"0") + x } Above, function strpad, if given a Number, returns either a Number or a String. This could cause later confusion. If a zero is added, there are two conversions of val to String. I am loath to change the type of an object that undergoes no change, e.g. if the function is passed something that evaluates to the Number 22, I don't want to spit out "22", although it really matters little, I suppose. |
|
These might be better; String is probably cheap when applied to a string :- function lz(s) { var t = String(s) return t.length==1 ? "0"+t : t } // or <2 ?? function lz(s) { var t return (t = String(s)).length==1 ? "0"+t : t } // ?? lz("K") returns "0K", but it's not OK, at least in my book. |
|
And I like the function to be somewhat descriptive, "strpad()" is a little obscure. |
![]() |
| Thread Tools | |
| Display Modes | |
| |