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

int main() 
{
    char s[100] ="4.0800" ; 

    printf("float value : %4.8f\n" ,(float) atoll(s)); 
    return 0; 
}

I expect the output should be 4.08000000 whereas I got only 4.00000000.

Is there any way to get the numbers after the dot?

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
abubacker
  • 4,638
  • 6
  • 32
  • 35

9 Answers9

74

Use atof() or strtof()* instead:

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

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
http://www.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.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 1
    printf("float value : %4.8f\n" , atof(s)); # output : float value : 0.00000000 – abubacker Oct 31 '11 at 07:58
  • 1
    Downvoted for recommending `atof`, which ignores parse errors, instead of `strtof`. You should know better ;-) – zwol Sep 10 '13 at 15:42
  • 1
    @Zack Can't say I knew that even existed two years ago. Especially since it seems to be a C99/C++11 function. – Mysticial Sep 10 '13 at 16:47
  • 2
    @Mysticial Oh, right, all the short-float functions were C99. But `strtod` is in C89 and has the same advantage over `atof` (unambiguously reporting parse errors). – zwol Sep 10 '13 at 16:51
  • 1
    Note that `atof()` returns a `double`, despite its name. The analogue is `strtod()` as `strtof()` returns `float`, and `strtold()` returns `long double`. – Jonathan Leffler Nov 04 '18 at 05:28
36

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.

sam hocevar
  • 11,853
  • 5
  • 49
  • 68
29

Why one should not use function atof() to convert string to double?

On success, atof() function returns the converted floating point number as a double value. If no valid conversion could be performed, the function returns zero (0.0). If the converted value would be out of the range of representable values by a double, it causes undefined behavior.

Refrence:http://www.cplusplus.com/reference/cstdlib/atof/

Instead use function strtod(), it is more robust.

Try this code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char s[100] = "4.0800";
    printf("Float value : %4.8f\n",strtod(s,NULL));
    return 0;
}

You will get the following output:

Float value : 4.08000000

ani627
  • 5,578
  • 8
  • 39
  • 45
5

Use atof()

But this is deprecated, use this instead:

const char* flt = "4.0800";
float f;
sscanf(flt, "%f", &f);

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

atof() returns 0 for both failure and on conversion of 0.0, best to not use it.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    Suggesting `sscanf()` instead isn't any better. I would suggest `strtod()` instead. – Jeff Mercado Oct 31 '11 at 07:49
  • 1
    printf("float value : %4.8f\n" , atof(s)); # output : float value : 0.00000000 – abubacker Oct 31 '11 at 07:56
  • "Use atof() But this is deprecated," --> please post a reference. – chux - Reinstate Monica Sep 30 '17 at 19:25
  • 1
    @chux Even if `atof()` isn't deprecated, it's imprecise, dangerous and ultimately useless for writing robust code. `atof()` should never be used. It has no way to return an error, and if the input is beyond the representable range undefined behavior is invoked. So the only way to safely use it is to fully parse the input some other way and make sure the input is valid before calling `atof()` - thus making `atof()` utterly useless. – Andrew Henle Nov 17 '18 at 13:37
  • 1
    @AndrewHenle No reason to consider `atof()` has any _precision_ difference than `strtod()` - so _imprecise-ness_ is not an issue. `atof()` has overflow UB as does `*scanf(flt, "%f", &f);` - all of which do not presently meet "robustness". `atof()` has limited uses: Example: Insuring a string is made up of (-,+,0-9,.) and has a length below, say 30, is trivially versus to re-writing `atof()`. Calling `atof()` on such a string has well defined behavior. IAC, as of C11 (and I think also C17), `atof()` is _not_ depreciated - so answer is factual incorrect & `sscanf()` here offers no improvement. – chux - Reinstate Monica Nov 17 '18 at 14:09
  • 1
    @chux I'd consider returning a valid value of 0.0 upon an error a form of imprecision. And given [the glibc implementation](https://code.woboq.org/userspace/glibc/stdlib/atof.c.html), rewriting `atof()` is, well, pretty trivial. *Insuring a string is made up of (-,+,0-9,.) and has a length below, say 30* My point is you only need to do that if you're using `atof()`, or some similarly-non-robust function such as one of the `*scanf()` family (which IMO are much worse in many ways than `atof()` - I deliberately wasn't commenting on the use of `scanf()` here...) – Andrew Henle Nov 18 '18 at 01:15
1

By using sscanf we can convert string to float.

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

int main() 
{
    char str[100] ="4.0800" ;     
    const char s[2] = "-";   
    char *token;
    double x;
   /* get the first token */ 
   token = strtok(str, s);
   sscanf(token,"%f",&x);
    printf( " %f",x );

    return 0; 
}
Kavita Jain
  • 90
  • 1
  • 9
1

Reminder: While using atof(), make sure you don't have "" in your string. atof("1.123") will return 0.000 or something like that.

Solution

str_val[0] = "0";
str_val[len-1] = "\n"; //len = length of string
0

You want to use the atof() function.

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/

mwp
  • 8,217
  • 20
  • 26
-2
double x;

char *s;

s = " -2309.12E-15";

x = atof(s);     /* x = -2309.12E-15 */

printf("x = %4.4f\n",x);
-3
Main()  {
    float rmvivek,arni,csc;
    char *c="1234.00";
    csc=atof(c);
    csc+=55;
    printf("the value is %f",csc);
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72