mackol Posted November 11, 2002 Share Posted November 11, 2002 does one exist? i know there is strtol and stuff like that but is there one that goes the other way thanks Link to comment Share on other sites More sharing options...
0 Gumboot Posted November 11, 2002 Share Posted November 11, 2002 How about sprintf()? Something like: #include <stdio.h> ... char dest[20]; float num = 3.141592653879; sprintf(dest, "%f", num); Link to comment Share on other sites More sharing options...
0 mackol Posted November 11, 2002 Author Share Posted November 11, 2002 uggh why didnt i think of that i was constantly stuck with the idea of using sscanf but that wasnt working so finally i created a custom function to check whether a input consists of at least 2 decimal integers let me post it float checkAid (char *aidStr) { float aid; char *endPtr; int x, y; x = strlen (aidStr); y = (strcspn (aidStr, ".")) + 1; if ((x - y) != 2) { printf("Please re-input the amount with exactly 2 decimal digits\n"); return 0; } aid = (float)(strtod(aidStr, &endPtr)); return aid; } with aidStr being a string containg the input from the user.. :) thanks mate Link to comment Share on other sites More sharing options...
0 mackol Posted November 11, 2002 Author Share Posted November 11, 2002 hey i found a fucntion, which is not ANSI C but which is for that purpose it is gcvt and gcvtf here is a link for the info http://www.redhat.com/docs/manuals/gnupro/...le_or_floa.html for those who are lazy to click on the link gvcvt, gcvtf [format double or float as string]-------------------------------------------------------------------------------- SYNOPSIS #include <stdlib.h> char *gcvt(double val, int precision, char *buf); char *gcvtf(float val, int precision, char *buf); DESCRIPTION gcvt writes a fully formatted number as a null-terminated string in the buffer, *buf. gcvtf produces corresponding character representations of float numbers. gcvt uses the same rules as the printf format, %.precisiong?only negative values are signed (with -), and either exponential or ordinary decimal-fraction format is chosen, depending on the number of significant digits (specified by precision). RETURNS The result is a pointer to the formatted representation of val (the same as the argument, buf). COMPLIANCE Neither function is ANSI C. Supporting OS subroutines required: close, fstat, isatty, lseek, read, sbrk, write. Link to comment Share on other sites More sharing options...
Question
mackol
does one exist?
i know there is strtol and stuff like that
but is there one that goes the other way
thanks
Link to comment
Share on other sites
3 answers to this question
Recommended Posts