We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How do i convert a string  into a float ?

sathishrdh
sathishrdh asked
on
Medium Priority
418 Views
Last Modified: 2012-05-11
Hi
  In my code ,i need to give an input value as a comand line parameter ie,a string value..
 i need to convert that string into a float value for my calculation...i need a help in this issue..

Thanks in Advance..
sathish
Comment
Watch Question

Senior Engineer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Sample code to show the usage of strtod:
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  char number[] = "3.141592";
  double pi = 0.0;
  pi = strtod(number, NULL);
  printf("pi = %lf\n", pi);
  return 0;
}
see below for the code and explanation of ssnkumar's comment

http://www.codecogs.com/reference/c/stdlib.h/strtod.php

Commented:
Also, you can look at this page, for the explanation of strtof():
http://www.java2s.com/Tutorial/C/0420__stdlib.h/strtof.htm
Todd GerbertSenior Engineer
CERTIFIED EXPERT
Top Expert 2010

Commented:
The beauty of sscanf, though, is that it doesn't necessarily require that the string with the float value contain only the float value:

#include <stdio.h>

int main(int argc, char *argv[])
{
	char cmdLineArg[] = "/FloatVal=2.3";
	float f;

	int parsed = sscanf(cmdLineArg, "/FloatVal=%f", &f);

	if (parsed)
		printf("The value provided with the FloatVal argument is: %f\n", f);
	else
		printf("Could not parse a float value from the provided string.\n");

	printf("Press ENTER to exit.");
	scanf("%*c");
}

Open in new window

Commented:
ModCorlEEone,

Thanks for the advice. I will keep that in mind and do as suggested.

Regards,
Narendra
Michael EagerConsultant

Commented:
Command line arguments are passed in argv, starting with index 1.  Assuming that the value is the first on the command line, after the program name, try the following:

#include <stdlib.h>
int main (int argc, char *argv[])
{
   double val;
   char *endp;
   /* include checking number of args. */
   val = strtod (argv[1], &endp);
   if (endp == argv[1] || *endp != '\0')
   {
       /* Handle error */
   }


evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.