Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-29-2006, 12:20 AM   #1 (permalink)
jaro
Registered User
 
jaro's Avatar
 
Join Date: Feb 2006
Posts: 9
jaro is on a distinguished road
String to Double

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
Quote:
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? Are there some bugs here that I'm not familiar with?

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

Regards,
dlare9
jaro is offline   Reply With Quote
Old 05-29-2006, 07:01 PM   #2 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
Quote:
Originally Posted by jaro
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? Are there some bugs here that I'm not familiar with?

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

Regards,
dlare9
Looking at the code, I don't see any reason why it wouldn't work, but it's a terribly innefficient solution.

Your output is correct, so just run it through some boundary cases and see what happens. However, just quickly looking at the code and it looks fine to me.
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for another program Androto Standard C, C++ 54 10-15-2004 08:21 AM
sorting objects arrays by object properties sde Java 28 08-05-2004 06:51 AM
From C to Java HighterDK Java 11 07-13-2004 08:15 PM


All times are GMT -8. The time now is 12:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting