Hello,

*Attaching herewith a routine to convert FP number to string.*

I would like to get feedback from those working on this task.
Whether this is what is required ?

If Yes, kindly guide me on the steps to contribute the same ?
If No, kindly suggest how to go about it ?

OR
Kindly let me know some other task to take up !!

Regards
*Bhaskar*
#include <stdio.h>
#include <malloc.h>

int double_to_string(double number, char* buffer, int size, int precision)
{
	int i = 0;
	
	if(precision > size)
		return 1;

	snprintf(buffer, size, "%lf", number);
	while(buffer[i])
	{
		if(buffer[i] == '.')
		{
			if((precision == 0) || (i == size - 2))
				buffer[i] = '\0';
			else
				buffer[i + precision + 1] = '\0';
			break;
		}
		i++;
	}
	printf("%s\n", buffer);
	
	return 0;
}

int main()
{
	char *buff = NULL;

	buff = (char*)malloc(15);
	double_to_string(-0.322837, buff, 14, 6);

	free(buff);
	return 0;
}

Reply via email to