Re: 4GL: Decimal type manipulation
Posted in 1998
drance@my-dejanews.com wrote: > > In article <361CEB60.1479@bayer.co.uk>, > Peter.Lancashire.PL1@bayer.co.uk wrote: > > > Why do you want to convert to an integer? Wouldn't a different printout > > of the same decimal value be enough? > > Thanks for the info guys. I got a bit bogged down in the problem - I guess I > should have asked a more open question. WHat I am actually trying to do is > display a decimal as "5.2", for example, if it is not a whole number and to > display "4", for example, if it is. So the display would look like this: > > 4.5 > 6 > 8 > 10 > 5.1 > > I'll try using the info you've given me to do this, but if there is an easier > way... > > Cheers, > Duncan I wrote a C wrapper to the printf("%g", doublevar) function to do just this. Here it is with the usual warnings and disclaimers: ------ cut here ----- /* --------------------------------------------------------------------------- %W% - format a number in printf style called from - Informix 4GL args - value to format, printf-style format string returns - string --------------------------------------------------------------------------- */ #include <stdio.h> #include <sqltypes.h> #include <local.h> #define BUFLEN 512 extern int fgl_printf(cargs) int cargs; { double value; /* value to format (as a double) */ char format[BUFLEN+1]; /* printf format string for a double */ char buf[BUFLEN+1]; /* buffer to do formatting in */ popquote(format, BUFLEN); popdub(&value); if (!risnull(CDOUBLETYPE, &value)) sprintf(buf, rstrip(format), value); else buf[0] = '\\0'; retquote(buf); return (1); } ------ cut here ----- The rstrip function is declared in local.h. It right strips (trailing) white space in place. -- Peter Lancashire Information Systems Specialist, Bayer plc Eastern Way, Bury St Edmunds, Suffolk, IP32 7AH, UK Tel: +44-1635-562258, Fax: +44-1635-562281 --- If all else fails, read the instructions and the release notes. Join Infuse, the UK Informix User Group at http://www.infuse.org.uk/ ---