Subject: USING keyword
Newsgroups: comp.databases.informix

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From: [email-masked]
Date: Sun, 17 Mar 96 14:40:17 GMT

Hi INFORMIX' ers

filed1 is decimal(10,6)
 filed1 may be up to 6 decimal or 2 decimal

How would I prevent trailing zeroes from being printed
(i.e. filed1=148.33 will be printed using ###.###### format as 148.330000)
I must use ###.###### format because it may be up to 6 decimal..

any hints.....

thanks........................

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From: [email-masked] (Colin McGrath)
Date: 21 Mar 1996 17:18:20 -0500

[email-masked] wrote:
>
> filed1 is decimal(10,6)
> filed1 may be up to 6 decimal or 2 decimal
>
> How would I prevent trailing zeroes from being printed
> (i.e. filed1=148.33 will be printed using ###.###### format as 148.330000)
> I must use ###.###### format because it may be up to 6 decimal..
>

Place the number in a character field and clean it up, as follows:

LET char_field1 = field1 using "##&.######"
FOR idx = 10 to 5 STEP -1
   IF char_field1[idx] != "0" THEN
      EXIT FOR
   ELSE
      LET char_field1[idx] = " "
   END IF
END FOR

-- 
Colin McGrath                             <Standard disclaimers apply>
Internet: [email-masked]   Voice: 215-422-4144   FAX: 215-422-1445

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From: [email-masked] (Jonathan Leffler)
Date: 21 Mar 1996 17:48:19 -0500

Put simply, you can't do it with USING.

I attach some code which I am sure I have previously published on
the net, which is different from the USING code.  It does not
actually have an option for suppressing trailing zeroes, but you
could easily provide one, somehow, if you are a C programmer.  If
not, maybe some kind soul will do it for you, but I wouldn't hold
my breath waiting.

Yours,
Jonathan Leffler ([email-masked]) #include <disclaimer.h>

>From: [email-masked]
>Date: Sun, 17 Mar 96 14:40:17 GMT
>X-Informix-List-Id: <news.22361>
>
>filed1 is decimal(10,6)
> filed1 may be up to 6 decimal or 2 decimal
>
>How would I prevent trailing zeroes from being printed
>(i.e. filed1=148.33 will be printed using ###.###### format as 148.330000)
>I must use ###.###### format because it may be up to 6 decimal..

:	"@(#)shar.sh	1.9"
#! /bin/sh
#
#	This is a shell archive.
#	Remove everything above this line and run sh on the resulting file.
#	If this archive is complete, you will see this message at the end:
#	"All files extracted"
#
#	Created: Thu Mar 21 14:07:51 PST 1996 by johnl at Informix Software Ltd.
#	Files archived in this archive:
#	lib/I4GL/src/decsci.c
#
#--------------------
if [ -f decsci.c -a "$1" != "-c" ]
then echo shar: decsci.c already exists
else
echo 'x - decsci.c (6953 characters)'
sed -e 's/^X//' >decsci.c <<'SHAR-EOF'
X/*
X@(#)File:            decsci.c
X@(#)Version:         1.7
X@(#)Last changed:    93/05/16
X@(#)Purpose:         Fixed, Exponential and Engineering formatting of DECIMALs
X@(#)Author:          J Leffler
X@(#)Copyright:       (C) JLSS 1991-2
X@(#)Product:         :PRODUCT:
X*/
X
X#include "esqlc.h"
X
X#define SIGN(s, p)  ((s) ? '-' : ((p) ? '+' : ' '))
X#define VALID(n)	(((n) <= 0) ? 6 : (((n) > 32) ? 32 : (n)))
X#define VALID2(n)	(((n) <= 0) ? 0 : (((n) > 151) ? 151 : (n)))
X
X/* For 32 digits, 3-digit exponent, leading blanks, etc, 42 is enough */
X/* With fixed format, could have -0.(130*0)(32 digits) + null for length 166 */
Xstatic char     buffer[166];
X
X#ifndef lint
Xstatic char     sccs[] = "@(#)decsci.c	1.7 93/05/16";
X#endif
X
X/*
X**	Format a fixed-point number.  Unreliable for ndigit > 58 because of the
X**	implementation of decfcvt in ${SOURCE}/infx/decconv.c
X*/
Xchar           *decfix(d, ndigit, plus)
Xdec_t          *d;
Xint             ndigit;
Xint             plus;
X{
X	register char  *dst = buffer;
X	register char  *src;
X	int             i;
X	int             sn;
X	int             dp;
X
X	if (risnull(CDECIMALTYPE, (char *)d))
X	{
X		*dst = '\0';
X		return(buffer);
X	}
X
X	ndigit = VALID2(ndigit);
X
X	src = decfcvt(d, ndigit, &dp, &sn);
X
X	*dst++ = SIGN(sn, plus);	/* Sign */
X	if (dp >= 1)
X	{
X		while (dp-- > 0)
X			*dst++ = ((*src) ? *src++ : '0');
X		if (ndigit > 0)
X			*dst++ = '.';
X		for (i = 0; i < ndigit; i++)
X			*dst++ = ((*src) ? *src++ : '0');
X	}
X	else
X	{
X		*dst++ = '0';
X		if (ndigit > 0)
X			*dst++ = '.';
X		i = 0;
X		while (dp++ < 0 && i < ndigit)
X		{
X			*dst++ = '0';
X			i++;
X		}
X		while (*src && i < ndigit)
X		{
X			*dst++ = *src++;
X			i++;
X		}
X	}
X	*dst = '\0';
X
X	return(buffer);
X}
X
X/* Format an exponent */
Xstatic char    *decexp(dst, dp)
Xregister char  *dst;
Xregister int    dp;
X{
X	*dst++ = 'E';
X	if (dp >= 0)
X		*dst++ = '+';
X	else
X	{
X		*dst++ = '-';
X		dp = -dp;
X	}
X	if (dp / 100 != 0)
X		*dst++ = dp / 100 + '0';
X	*dst++ = (dp / 10) % 10 + '0';
X	*dst++ = (dp % 10) + '0';
X	if (dp / 100 == 0)
X		*dst++ = ' ';
X	*dst = '\0';
X	return(dst);
X}
X
X/*	Format a scientific notation number */
Xchar           *decsci(d, ndigit, plus)
Xdec_t          *d;
Xint             ndigit;
Xint             plus;
X{
X	register char  *dst = buffer;
X	register char  *src;
X	int             sn;
X	int             dp;
X	dec_t           z;
X
X	if (risnull(CDECIMALTYPE, (char *)d))
X	{
X		*dst = '\0';
X		return(buffer);
X	}
X
X	ndigit = VALID(ndigit);
X	src = dececvt(d, ndigit, &dp, &sn);
X	*dst++ = SIGN(sn, plus);	/* Sign */
X	*dst++ = *src++;			/* Digit before decimal point */
X	*dst++ = '.';				/* Decimal point */
X	while (*src)				/* Digits after decimal point */
X		*dst++ = *src++;
X	deccvdbl(0.0, &z);
X	dst = decexp(dst, dp - (deccmp(d, &z) == 0));	/* Exponent */
X	return(buffer);
X}
X
X/*
X**	Format an engineering notation number.
X**	Exponent is always a power of three.  Exponent is omitted if it is zero.
X**	For values 1.0E-1 <= ABS(x) < 1.0E0, the value is printed as 0.xxxx.
X**	The field always aligns the decimal points, prefixing blanks if necessary.
X**	The number of digits printed ND is such that if the power of ten is of
X**	the form (n * 3 + m) (m = 0, 1, 2), ND = ndigit - 2 + m.  The exception
X**	to this is when the value is treated as 0.xxxx and ND = ndigit - 2
X**	including the leading zero!
X**	The field can be made constant width by specifying a non-zero cw.
X*/
Xchar           *deceng(d, ndigit, plus, cw)
Xdec_t          *d;
Xint             ndigit;
Xint             plus;
Xint             cw;
X{
X	register char  *dst = buffer;
X	register char  *src;
X	int             sn;
X	int             dp;
X	int             lb;
X	int             exp;
X
X	if (risnull(CDECIMALTYPE, (char *)d))
X	{
X		*dst = '\0';
X		return(buffer);
X	}
X
X	ndigit = VALID(ndigit);
X	src = dececvt(d, ndigit, &dp, &sn);
X	exp = dp - 1;
X	/* Calculate leading blanks */
X	lb = 2 - (exp % 3);
X	if (lb >= 3)
X		lb -= 3;
X	if (exp == -1)
X	{
X		lb = 2;
X		ndigit--;
X	}
X	/* Shorten digit string as necessary */
X	src[ndigit - lb] = '\0';
X
X	while (lb-- > 0)			/* Leading blanks */
X		*dst++ = ' ';
X
X	*dst++ = SIGN(sn, plus);	/* Sign */
X	if (exp == -1)
X	{							/* Leading 0 */
X		*dst++ = '0';
X		exp = 0;
X	}
X	else
X	{							/* Leading digits */
X		while (exp % 3 != 0)
X		{
X			*dst++ = ((*src) ? *src++ : '0');
X			exp--;
X		}
X		*dst++ = *src++;
X	}
X	*dst++ = '.';				/* Decimal point */
X	while (*src)				/* Digits after decimal point */
X		*dst++ = *src++;
X	if (exp != 0)				/* Exponent */
X		dst = decexp(dst, exp);
X	else if (cw)
X	{
X		for (lb = 0; lb < 5; lb++)
X			*dst++ = ' ';
X	}
X	*dst = '\0';
X	return(buffer);
X}
X
X#ifdef TEST
X
X#define DIM(x)	(sizeof(x)/sizeof(*(x)))
X
Xstatic char    *values[] =
X{
X "0",
X "+3.14159265358979323844e+00",
X "-3.14159265358979323844e+01",
X " 3.14159265358979323844e+02",
X "+3.14159265358979323844e+03",
X "-3.14159265358979323844e+34",
X " 3.14159265358979323844e+68",
X "+3.14159265358979323844e+99",
X "-3.14159265358979323844e+100",
X " 9.99999999999999999999e+125",
X "+1.00000000000000000000e+126",
X "-3.14159265358979323844e+00",
X " 3.14159265358979323844e-01",
X "+3.14159265358979323844e-02",
X "-3.14159265358979323844e-03",
X " 3.14159265358979323844e-34",
X "+3.14159265358979323844e-68",
X "-3.14159265358979323844e-99",
X " 3.14159265358979323844e-100",
X "+3.14159265358979323844e-126",
X "-3.14159265358979323844e-127",
X " 1.00000000000000000000e-128",
X "+1.00000000000000000000e-129",
X "-1.00000000000000000000e-130",
X " 9.99999999999999999999e-131",
X};
X
Xmain()
X{
X	char           *s;
X	dec_t           d;
X	int             i;
X	int             err;
X
X	printf("\nFixed-point notation\n");
X	printf("%-30s %s\n", "Input value", "Formatted");
X	for (i = 0; i < DIM(values); i++)
X	{
X		if ((err = deccvasc(values[i], strlen(values[i]), &d)) != 0)
X			printf("deccvasc error %d on %s\n", err, values[i]);
X		else
X		{
X			s = decfix(&d, 6 + 3 * i, i % 2);
X			printf("%-30s :%s:\n", values[i], s);
X		}
X	}
X
X	printf("\nScientific notation\n");
X	printf("%-30s %s\n", "Input value", "Formatted");
X	for (i = 0; i < DIM(values); i++)
X	{
X		if ((err = deccvasc(values[i], strlen(values[i]), &d)) != 0)
X			printf("deccvasc error %d on %s\n", err, values[i]);
X		else
X		{
X			s = decsci(&d, 6, i % 2);
X			printf("%-30s :%s:\n", values[i], s);
X		}
X	}
X
X	printf("\nEngineering notation (variable)\n");
X	printf("%-30s %s\n", "Input value", "Formatted");
X	for (i = 0; i < DIM(values); i++)
X	{
X		if ((err = deccvasc(values[i], strlen(values[i]), &d)) != 0)
X			printf("deccvasc error %d on %s\n", err, values[i]);
X		else
X		{
X			/*s = deceng(&d, 16 + (i / 3), i % 2, 0);*/
X			s = deceng(&d, 16, i % 2, 0);
X			printf("%-30s :%s:\n", values[i], s);
X		}
X	}
X
X	printf("\nEngineering notation (constant)\n");
X	printf("%-30s %s\n", "Input value", "Formatted");
X	for (i = 0; i < DIM(values); i++)
X	{
X		if ((err = deccvasc(values[i], strlen(values[i]), &d)) != 0)
X			printf("deccvasc error %d on %s\n", err, values[i]);
X		else
X		{
X			s = deceng(&d, 32 - 3 * (i / 3), i % 2, 1);
X			printf("%-30s :%s:\n", values[i], s);
X		}
X	}
X
X	return(0);
X}
X
X#endif	/* TEST */
SHAR-EOF
chmod 444 decsci.c
if [ `wc -c <decsci.c` -ne 6953 ]
then echo shar: decsci.c unpacked with wrong size
fi
# end of overwriting check
fi
echo All files extracted
exit 0

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From: [email-masked] (Brent Jackson)
Date: Sun, 24 Mar 1996 21:49:26 GMT

[email-masked] (Jonathan Leffler) wrote:

>Put simply, you can't do it with USING.

>>From: [email-masked]
>>Date: Sun, 17 Mar 96 14:40:17 GMT
>>X-Informix-List-Id: <news.22361>
>>
>>filed1 is decimal(10,6)
>> filed1 may be up to 6 decimal or 2 decimal
>>
>>How would I prevent trailing zeroes from being printed
>>(i.e. filed1=148.33 will be printed using ###.###### format as 148.330000)
>>I must use ###.###### format because it may be up to 6 decimal..

If you are using 4GL then I think you can do it with USING, but you require
an if statement and two USING clauses.  Something like :

	LET temp_value = filed1 * 100
	IF (temp_value * 10000) = (filed1 * 1000000) THEN
		PRINT filed1 USING "###.##"
	ELSE
		PRINT filed1 USING "###.######"
	END IF

This is untested, but should give you the general idea.

Cheers,
        Brent Jackson.

[email-masked]   Phone : 64-9-3603593  Fax : 64-9-3602840

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++