| Date: | Mon, 9 Aug 2010 12:01:26 -0700 |
| Reply-To: | Bruce Weaver <bruce.weaver@hotmail.com> |
| Sender: | "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU> |
| From: | Bruce Weaver <bruce.weaver@hotmail.com> |
| Subject: | Re: leading zeros to strings |
| In-Reply-To: | <7736771.35210.1281369455608.JavaMail.coxspss@127.0.0.1> |
| Content-Type: | text/plain; charset=us-ascii |
David Wright-6 wrote:
>
> I have a numeric variable that I need to have leading zeros on.
> However, when I use the N format options it strips the decimal point.
>
>
> Original varaible X ranges from 0 to 99.99 (n8.2).
>
> examples:
> 82.17
> 1.07
> 0
>
> Alter type X (n5.2). (produces the following:
> 08217
> 00107
> 00000
>
> I need to have:
> 82.17
> 01.07
> 00000
>
> For some reason I'm losing the decimal. What am I doing wrong? I could
> change these into strings since I'm uploading into an oracle table with
> a nvarchar(5) format but still need the leading zeros.
>
> Thanks in advance.
>
> David
>
>
That is a bit curious. But I get the same thing you do. For the case where
X = 0, do you really want the string to be '00000', or should it be '00.00',
given that the other cases have two decimals? See below for some possible
work-arounds.
data list free / x (f8.2).
begin data
82.17 1.07 0
end data.
list.
Output:
x
82.17
1.07
.00
alter type x(n5.2).
list.
Output:
x
08217
00107
00000
* As David observes, the decimal is not displayed.
* But it is still there -- notice what happens when
* we convert back to an F5.2 format.
alter type x(f5.2).
list.
Output:
x
82.17
1.07
.00
* Compute a copy of X and format it F5.2 .
compute X2 = X.
format X2 (f5.2).
exe.
* Now convert X2 to string, and then Left-Pad it with 0's .
alter type x2 (a5).
COMPUTE x2=CHAR.LPAD(ltrim(x2),5,'0').
list.
Output:
x X2
82.17 82.17
1.07 01.07
.00 00.00
if (X EQ 0) X2 = '00000'. /* if 0 really has to be '00000' .
list.
Output:
x X2
82.17 82.17
1.07 01.07
.00 00000
HTH.
-----
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/
"When all else fails, RTFM."
NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/leading-zeros-to-strings-tp2269030p2276813.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
|