Use atof() or strtof()* instead:

printf("float value : %4.8f\n" ,atof(s)); 
printf("float value : %4.8f\n" ,strtof(s, NULL)); 

https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/

  • atoll() is meant for integers.
  • atof()/strtof() is for floats.

The reason why you only get 4.00 with atoll() is because it stops parsing when it finds the first non-digit.

*Note that strtof() requires C99 or C++11.

Answer from Mysticial on Stack Overflow
Top answer
1 of 8
80

Use atof() or strtof()* instead:

printf("float value : %4.8f\n" ,atof(s)); 
printf("float value : %4.8f\n" ,strtof(s, NULL)); 

https://cplusplus.com/reference/cstdlib/atof/
https://cplusplus.com/reference/cstdlib/strtof/

  • atoll() is meant for integers.
  • atof()/strtof() is for floats.

The reason why you only get 4.00 with atoll() is because it stops parsing when it finds the first non-digit.

*Note that strtof() requires C99 or C++11.

2 of 8
38

Unfortunately, there is no way to do this easily. Every solution has its drawbacks.

  1. Use atof() or strtof() directly: this is what most people will tell you to do and it will work most of the time. However, if the program sets a locale or it uses a library that sets the locale (for instance, a graphics library that displays localised menus) and the user has their locale set to a language where the decimal separator is not . (such as fr_FR where the separator is ,) these functions will stop parsing at the . and you will stil get 4.0.

  2. Use atof() or strtof() but change the locale; it's a matter of calling setlocale(LC_ALL|~LC_NUMERIC, ""); before any call to atof() or the likes. The problem with setlocale is that it will be global to the process and you might interfer with the rest of the program. Note that you might query the current locale with setlocale() and restore it after you're done.

  3. Write your own float parsing routine. This might be quite quick if you do not need advanced features such as exponent parsing or hexadecimal floats.

Also, note that the value 4.08 cannot be represented exactly as a float; the actual value you will get is 4.0799999237060546875.

๐ŸŒ
Particle
community.particle.io โ€บ getting started
Best way to convert float to String? - Getting Started - Particle
October 13, 2016 - Hi all, I've been poking around this forum as well as stack overflow for c/c++ and am a bit confused on the best way to convert a float (or other) type to a String for publishing variables to the particle cloud. Isโ€ฆ
๐ŸŒ
GitHub
github.com โ€บ tedtoal โ€บ floatToString
GitHub - tedtoal/floatToString: C code to create function floatToString() that converts a float to a string with specified number digits after dp.
This library provides a C code function named floatToString() that converts a float (floating point) value to a string with specified number digits after the decimal point. It rounds the value to the nearest least significant digit.
Author ย  tedtoal
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ convert-a-floating-point-number-to-string-in-c
Convert a floating point number to string in C
Step 1 โˆ’ Take a number from the user Step 2 โˆ’ Create an empty string buffer to store result Step 3 โˆ’ Use sprintf() to convert number to string Step 4 โˆ’ End ... #include<stdio.h> main() { char str[20]; //create an empty string to store number float number; printf("Enter a number: "); ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ convert-floating-point-number-string
Convert a floating point number to string in C - GeeksforGeeks
July 23, 2025 - Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa().
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ itzadam5x
C Programming Tutorial 74, Converting Ints and Floats to Strings - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published ย  June 24, 2012
Views ย  32K
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c โ€บ convert-float-value-to-string-using-gcvt-in-c-language.aspx
Convert float value to string using gcvt() in C language
It's a library function of stdio.h header, this function is used to convert a floating point number to string. ... Here, double value : is the float/double value int ndigits : number of digits including point (decimal point), for example if you want to get value in xx.yyy format then it should be 6 char * buf : character pointer, in this variable string converted value will be copied.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ converting a string to a float without using atof
r/C_Programming on Reddit: Converting a string to a float without using atof
March 5, 2018 -

I'm supposed to convert a string into a float without using any standard library function, and I can't figure out why this doesn't work.

float strToFloat(const char string[])
{
	int i;
	int j = 0;
	int ten = 1;
	float dec = 10;
	float result = 0;

	for (i = 0; string[i] != '\0'; ++i)
	{
		if (string[i] == '.')
		{
			for (; string[i] != '\0'; ++i)
			{
				dec = (dec * 10) + string[i] - '0';
				++j;
			}
		}
		else
			result = (result * 10) + string[i] - '0';
	}

	for (; i != 0; --i)
	{
		ten *= 10;
	}

	dec /= ten;
	printf("%d", dec);
	result += dec;
	
	return result;
}

Could someone help me?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ gcvt-convert-float-value-string-c
gcvt() | Convert float value to string in C - GeeksforGeeks
December 22, 2017 - gcvt (float value, int ndigits, char * buf); float value : It is the float or double value. int ndigits : It is number of digits. char * buf : It is character pointer, in this variable string converted value will be copied.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-float-into-string-in-C
How to convert float into string in C - Quora
Answer (1 of 6): If C90 sprinf(): sprintf(mybyf, "%f", thefloat); if C99, then snprintf might be preferable: snprintf(mybuf, BUF_SIZE, "%f", thefloat); if C11 then there's sprintf_s, snprintf_s which act same as their non _s counterparts, but with some extra eerror detection.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-a-floating-point-number-to-string-in-C-programming-language
How to convert a floating point number to string in C programming language - Quora
Answer (1 of 2): snprintf can convert anything to a string adding whatever formatting you specify. [code]char buf[100]; snprintf(buf, sizeof buf, โ€œ%fโ€, x); [/code]There are other string functions, but snprintf is the only one you need.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Convert Float Into String Sprint F | Basic Programming Tutorial - YouTube
Kitflix has currently more than 5000 students from 150+ countries. Weโ€™re slowly progressing towards becoming a community of like minded people who love to ex...
Published ย  June 2, 2023
๐ŸŒ
Arm Community
community.arm.com โ€บ support-forums โ€บ f โ€บ keil-forum โ€บ 20270 โ€บ code-for-converting-float-to-string
code for converting float to string - Keil forum
Have a question? If you can, please take a moment to also see if there is a question that you are able to answer ยท These cookies are necessary for the website to function properly, and if you elect to block them, some parts of the site may not work
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 180712-anyone-knows-how-i-can-convert-float-double-string.html
Anyone knows how I can convert a float/double to string?
After that, you can multiply the fractional part by 10, get the integer part and subtract it, keeping in loop until get zero (or until you are satisfied)... Exemple: ... 0.725 * 10 = 7.25 (print 7 and keep 0.25 0.25 * 10 = 2.5 (print 2 and keep 0.5) 0.5 * 10 = 5.0 ( print 5 and keep 0.0 ) 0.0 (end) Last edited by flp1969; 12-04-2021 at 06:33 AM. ... If you want some papers to study some refined algorithms for floating point printing.
๐ŸŒ
Post.Byes
post.bytes.com โ€บ home โ€บ forum โ€บ topic
How do i convert a float to string? - Post.Byes
You will notice in this example that if you run it the string is reported as truncated. This is because by default the %f format has a percision of 6 decimal places. Change %f to %.2f and rerun it. ... #include <stdio.h> #define SIZE 10 int main(void) { char buf[SIZE]; int result = 0; float value = 154.78; result = snprintf(buf, SIZE, "%f", value); if (result >= SIZE) printf("The string has been truncated\n"); printf("The string value of the floating value = %s\n", buf); return 0; }
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 463736372 โ€บ Convert-a-floating-point-number-to-string-in-C
Convert Float to String in C | PDF | Integer (Computer Science) | String (Computer Science)
The document describes a C function called ftoa() that converts a floating-point number to a string. It takes the number, an array to store the string, and the number of digits after the decimal as parameters.