www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/02/21/04:48:17

From: kunst AT prl DOT philips DOT nl
Subject: Re: float to string
To: HERZER AT rz-nov2 DOT rz DOT FH-Weingarten DOT DE (Herzer Armin Assi)
Date: Tue, 21 Feb 1995 10:04:41 +0100 (MET)
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu (DJGPP users list)

> 
> is there a function that converts a float into a string (like 
> ecvt, fcvt, and gcvt in TURBO C). I am converting "by hand" and want 
> to know if there are some changes to the compiler now (wasn't there 
> an e-,f-,gcvt entry in the info reader for GNU C 2.5.8? When I look 
> into the new Info reader (that comes with GCC 2.6.x) I miss these 
> entries [and I remember that I tried to use these functions with 
> 2.5.8 without any success -> undefined references errors]).
> 
> BTW: The TURBO C manual claims these funcions as "available on UNIX-
> Systems".
> 

The functions ecvt, fcvt, gvct:

  char *ecvt(double value, size_t ndigit, int *decpt, int *sign);
  char *fcvt(double value, size_t ndigit, int *decpt, int *sign);
  char *gcvt(double value, size_t ndigit, char *buf);

are *not* ANSI C functions (i.e. not portable). They only conform to XPG2.

You can use sprintf to convert a float into a string.


#include <stdio.h>

int main()
{
  char buf[100];
  float f = 123.45;

  sprintf (buf, "%8.3f", f);
  printf ("float f is '%s'.\n", buf);
  return 0;
}  

This will print:
float f is ' 123.450'.



Regards,

Pieter Kunst (kunst AT prl DOT philips DOT nl)


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019