Thread: String to Double function

  1. #1
    Registered User jaro's Avatar
    Join Date
    Mar 2006
    Location
    In my Parent House
    Posts
    34

    String to Double function

    Hi to all!,

    I have a function that convert a string numeral (entered by a user) to a double value, having rounded its decimal value to (in this case) 4 significant places.

    here is the code
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int isNumber(const char str[])
    {
    	char *ok;
    	strtod(str,&ok);
    	return !isspace(*str) && strlen(ok)==0;
    }
    
    
    double stringToDouble(char *digit , size_t decPlace) // int dec is the number of decimal places
    {
    
    	double result = 0;
    	char *ptr;
    	char temp1[10]="";
    	char temp2[15]="";
    
    	if(digit != NULL && isNumber(digit)) // check if string is not NULL and a valid number
    	{
    		result = strtod(digit,&ptr);
    
    		sprintf(temp1,"%%.%df",decPlace);
    		sprintf(temp2,temp1,result);
    		
    		result = strtod(temp2,&ptr);
    
    		
    	}else
    	{
    		printf("FAILED:The String (%s) cannot be converted to double: result -> %g\n",digit, result);
    	}
    
    	return result;
    }
    
    
    
    int main()
    {
    	double num ;
    	char numberInString[100];
    	int decPlace = 4;
    	
    	//for testing purpose only
    	printf("Enter a number\n");
    	fgets(numberInString, sizeof(numberInString), stdin);
    		if (numberInString[strlen(numberInString)-1] == '\n') 
    		{   
    			numberInString[strlen(numberInString)-1] = '\0';//remove the new-line
    		}
    
    	printf("the string %s\n",numberInString);
    	
    	num =stringToDouble(numberInString,decPlace);
    
    	printf("is converted to double with value of %f with %d significant decimal places\n",num,decPlace);
    
    
    	return 0;
    
    }

    OUTPUT
    Enter a number
    34.56789
    the string 34.56789
    is converted to double with value of 34.567900 with 4 significant decimal places
    My question is with regards to stringToDouble().
    I've used two temporary character array, temp1 and temp2 respectively, in order to manipulate the output of stringToDouble
    in order for the stringToDouble to return the right value (with decimal place included).
    Does this solution valid?

    strtod() only returns the string in double form.AFAIK it can't round off value (decimal places).

    Regards,
    jaro

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I must say I don't see how this is supposed to be different or better than strtod(), especially since you call it in your function a couple times. Instead of trying to force a number to have four decimal places only, numbers usually round themselves if you specify the precision for output.
    Code:
    #include <stdio.h>
    
    int main(void) {
        double d = 34.56789;
        int decPlaces = 4;
        printf("%.*f\n", decPlaces, d);
        getchar();
        return 0;
    }
    
    /* 
    34.5679
     */
    Last edited by whiteflags; 05-26-2006 at 06:35 AM.

  3. #3
    Registered User jaro's Avatar
    Join Date
    Mar 2006
    Location
    In my Parent House
    Posts
    34
    Quote Originally Posted by citizen
    I must say I don't see how this is supposed to be different or better than strtod(), especially since you call it in your function a couple times. Instead of trying to force a number to have four decimal places only, numbers usually round themselves if you specify the precision for output.
    the function is not really for display purposes only.
    the program that I'm currently working on is suppose to read a file and store it to the database. and I need to convert some string to numbers (in this case double) in order to be saved to the database.

    thanks for the quick reply
    -jaro

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In that case, since your solution works, you can use it. As an alternative, you can study how strtod() really works (and also float-point representation in binary) and roll your own function to stop a certain number of places after a decimal, but you don't need to do that.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    strtod() only returns the string in double form.AFAIK it can't round off value (decimal places).
    Use a function like this to round a number:
    Code:
    double round2places(double n) {
        return ((int)(n * 100 + .5))/100.0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM