Bug or Not? Taking STRING of a string whilst making leading zeros
|
01-29-2018, 04:41 PM
(This post was last modified: 01-29-2018 07:11 PM by StephenG1CMZ.)
Post: #1
|
|||
|
|||
Bug or Not? Taking STRING of a string whilst making leading zeros
I was trying to add some leading zeros into a string.
I discovered that if there is an unneccessary call to STRING the leading 0 doesnt appear. Why aren't "9" and STRING("9") both of length 1, yielding 1 leading zero in the example code? Code:
Is there a better way to get leading zeros in a string? Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
01-29-2018, 05:21 PM
Post: #2
|
|||
|
|||
RE: Bug: Taking STRING of a string whilst making leading zeros
Ans → 9
STRING(Ans) → "9" STRING(Ans) → ""9"" STRING(Ans) → """"9"""" STRING(Ans) → """"""""9"""""""" Viga C | TD | FB |
|||
01-30-2018, 11:38 AM
Post: #3
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
(01-29-2018 04:41 PM)StephenG1CMZ Wrote: Why aren't "9" and STRING("9") both of length 1, yielding 1 leading zero in the example code?"9" is a string of length 1, but STRING("9") is a string of length 3: '9' plus 2 double quote characters. (01-29-2018 04:41 PM)StephenG1CMZ Wrote: Is there a better way to get leading zeros in a string?A simple way to get a string with N zeros: tail(STRING(10^N)), or if you prefer not to use a CAS function: MID(STRING(10^N),2,N). It works for N>=0, returning "" for N=0. |
|||
01-30-2018, 12:53 PM
(This post was last modified: 01-30-2018 02:58 PM by toml_12953.)
Post: #4
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
(01-29-2018 04:41 PM)StephenG1CMZ Wrote: Is there a better way to get leading zeros in a string? If the number is N and length you want is X, I use: Code: RIGHT(MID(STRING(10^X,1),2)+N,X) Thanks to Joe's post, I changed the STRING function to include the format type. Tom L Cui bono? |
|||
01-30-2018, 02:28 PM
Post: #5
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
Warning #1: The output of STRING(X) depends on the current Number Format in Home Settings. For example, STRING(456) in Fixed 2 mode returns "456.00" which might not be what you want. To insure that whole numbers always get turned into strings without decimal places, use the optional second argument to specify the desired format regardless of the current setting. STRING(456,1) --> "456" (the 1 here means "use Standard number format").
Warning #2: In CAS programs, be sure to use the newer, smarter function STRING(), not its older, dumber sibling string(). Thankfully, Home always uses STRING(). <0|ɸ|0> -Joe- |
|||
01-30-2018, 03:00 PM
Post: #6
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros | |||
01-30-2018, 03:25 PM
Post: #7
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
(01-30-2018 03:00 PM)Didier Lachieze Wrote:(01-30-2018 02:28 PM)Joe Horn Wrote: Warning #1: The output of STRING(X) depends on the current Number Format in Home Settings. Moi aussi. (Me too™) Tom L Cui bono? |
|||
01-30-2018, 07:58 PM
(This post was last modified: 01-30-2018 08:00 PM by StephenG1CMZ.)
Post: #8
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
Thanks for those tips - I was assuming STRING was cleverly turning whatever into a string and could leave "9" unchanged.
So far I have this for producing leading zeros (based on the above but handling 0 zeros): Code:
I am surprised there is no built-in for this. Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
01-30-2018, 08:16 PM
(This post was last modified: 01-30-2018 08:17 PM by Didier Lachieze.)
Post: #9
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
Here is a version without the limitation of NZ<12:
Code: EXPORT NZEROS(NZ) (01-30-2018 02:28 PM)Joe Horn Wrote: Warning #2: In CAS programs, be sure to use the newer, smarter function STRING(), not its older, dumber sibling string(). Thankfully, Home always uses STRING().What's the difference between the two functions? |
|||
01-30-2018, 08:17 PM
Post: #10
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
Either make a "constant" string of more-than-enough 0's and append it to the front of your number, or create a function that will produce such a string by repeatedly adding a small string of zero's to itself and then append this to the front. Then simply "cut" the desired length using MID().
Graph 3D | QPI | SolveSys |
|||
01-30-2018, 08:48 PM
Post: #11
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
(01-30-2018 08:16 PM)Didier Lachieze Wrote: Here is a version without the limitation of NZ<12: Neat solution. I am surprised that the obvious recursive solution is faster (and can recurse up to at least 1000 digits): Code:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
01-30-2018, 09:19 PM
Post: #12
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
(01-30-2018 08:17 PM)Han Wrote: Either make a "constant" string of more-than-enough 0's and append it to the front of your number, or create a function that will produce such a string by repeatedly adding a small string of zero's to itself and then append this to the front. Then simply "cut" the desired length using MID().Yup, that what the line I posted does. It works and is pretty easy to understand. Tom L Cui bono? |
|||
01-30-2018, 09:36 PM
(This post was last modified: 01-30-2018 09:37 PM by Carlos295pz.)
Post: #13
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
The recursive function may look like this:
Code: EXPORT ZEROS(NZ) Speed may not be important for that code, but it may be better to use ITERATE instead of tail. Code: EXPORT ZEROS(NZ) Viga C | TD | FB |
|||
01-31-2018, 03:05 AM
Post: #14
|
|||
|
|||
RE: Bug or Not? Taking STRING of a string whilst making leading zeros
Some interesting solutions I might not have thought of myself.
Here are two functions I've been using to left- or right-justify a string. Arguments are a string to be processed, the desired length of the resulting string, and the filler character, could be anything but probably 0 or space most common. T Code: StrLJust(str,len,spacer) But looking at the ITERATE command, this can definitely be made more compact: Code: StrLJust(str,len,spacer) Thanks Carlos! Have you made benchmark comparisons between FOR loop and ITERATE? Would be curious if one is faster than the other. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)