[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do i convert a string  into a float ?

Posted on 2011-04-28
11
Medium Priority
?
405 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
0
Comment
Question by:sathishrdh
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1000 total points
ID: 35482819
0
 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 1000 total points
ID: 35482921
try atof or strtod
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35483286
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;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:point_pleasant
ID: 35483323
see below for the code and explanation of ssnkumar's comment

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

Expert Comment

by:ssnkumar
ID: 35483376
Also, you can look at this page, for the explanation of strtof():
http://www.java2s.com/Tutorial/C/0420__stdlib.h/strtof.htm
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35483737
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

0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35489303
ModCorlEEone,

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

Regards,
Narendra
0
 
LVL 8

Expert Comment

by:eager
ID: 35492352
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 */
   }


0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419111
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question