COHERENT manpages

This page displays the COHERENT manpage for ecvt() [Convert floating-point numbers to strings].

List of available manpages
Index


ecvt() -- General Function (libc)

Convert floating-point numbers to strings
char *
ecvt(d, prec, dp, signp)
double d; int prec, *dp, *signp;

ecvt()  converts  d  into a  NUL-terminated  string  of  numerals with  the
precision of prec. Its operation resembles that of printf()'s operator %e.

ecvt()  rounds the  last digit  and returns  a pointer  to the  result.  On
return, ecvt() sets  dp to point to an integer  that indicates the location
of the decimal point relative to  the beginning of the string, to the right
if positive, to the left if negative.  It sets signp to point to an integer
that indicates the sign of d, zero if positive and nonzero if negative.

Example

The following program demonstrates ecvt(), fcvt(), and gcvt().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* prototypes for extended functions */
extern char *ecvt();
extern char *fcvt();
extern char *gcvt();

main(void)
{
    char buf[64];
    double d;
    int i, j;
    char *s;

    d = 1234.56789;
    s = ecvt(d, 5, &i, &j);
    /* prints ecvt="12346" i=4 j=0 */
    printf("ecvt=\"%s\" i=%d j=%d\n", s, i, j);

    strcpy(s, fcvt(d, 5, &i, &j));
    /* prints fcvt="123456789" i=4 j=0 */
    printf("fcvt=\"%s\" i=%d j=%d\n", s, i, j);

    s = gcvt(d, 5, buf);
    /* prints gcvt="1234.56789" */
    printf("gcvt=\"%s\"\n", s);
}

See Also

libc

Notes

ecvt() performs conversions within static string buffers that it overwrites
with each execution.