how can i convert string os size[10] to float. also i cannot use atof or sscanf. im asked to use bitwise or anyother methos which will take less processiong time.
is there anyway. thank you

there's a function in <cstdlib> called strtof (string to float) which can perfectly do that...

you could convert one character at a time. start out by multiplying the result by 10 add the character and subtract '0', similar to how you would convert an integer

char inputstr[] = "123.45";
float result = 0.0F;

// convert 1st character
result = (result * 10) + inputstr[i] - '0';

That will work for all the characters that preceed the decimal point. After that you need another float, do similar math as above (but you don't need to multiply by 10), divide the result by (10 * number of decimal digits) and finally add that to the result. Example:

char inputstr[] = "123.45";
float result = 0.0F;
float temp;
int nDecimalDigits;'
// convert 1st character
nDecimalDigits = 1; // initialize this outside the loop



temp = inputstr[i] - '0';
nDecimalDigits *= 10;
temp /= nDecimalDigits;
result += temp;

What's not shown above is that you have to put that code in a for-next loop with i as the loop counter.

Confused now?

sorry sir, im bit confused rite now. i dun really follow ur code. im sorry.
actually my string is dis. out[]="0303.4578"
i need to convert to float i=0303.4578.
thank you.:S

If you can't use build-in standard C functions, then you need to figure out what I posted.

Any decimal digit in a string can be converted to its binary equlivalent by simply subtracting the ascii value of '0'. For example, '1' - '0' = 1. If you look up the values of '1' and '0' in a standard ascii chart you will find that a '1' has a decimal value of 49, and a '0' has a decimal value of 48. So 49 - 48 = 1.

After converting the digit to binary as previously explained you have to add it to the previous value of the final result. First shift all the digits previously converted to the left by 1 place, which is done by simply multiplying the value by 10, when add the value of the new decimal digit.

Now loop through all the digits in the string you posted and do the math as I illustrated previously for each digit. Give it a try and post your code (in code tags), and ask lots of questions if you need to.

dear Ancient Dragon ,
i think this is code as u explained to me so i tried out the code not sure which to print out so printed out all the results.

/********************************************/
main()
{
char inputstr[]="123.45";
float result= 0.0F;
float temp;
int ndecimaldigit,i;

ndecimaldigit = 1;

for(i=0;i<=4;i++)
{
temp=inputstr[i] - '0';
ndecimaldigit *= 10;
temp /= ndecimaldigit;
result += temp;
}
printf("%f %f %d \n",result,temp,ndecimaldigit);
}
/*********************/

but the o\p i got is
0.122671 -0.000129 -31072

wht mistake did i perform?

thank you for ur time n suggestion.:-O

dear Ancient Dragon ,

You want to make Ancient Dragon happy?. Learn here how to properly tag the code you post.

i think this is code as u explained to me so i tried out the code not sure which to print out so printed out all the results.

wht mistake did i perform?

thank you for ur time n suggestion.:-O

Also read these rules about posting.
That would be a good way of saying thanks.

You need two loops, not 1.
For all digits before the decimal, you need to multiply the total by 10 then add the value. Byt the way use a while loop.

When you get to the decimal you need to start a new loop to deal with the characters after.

why dn't you use a much simple (though longer) way to do this... this would be the "amateur way":

char string[]="31.245ยจ;
char num;
double div=1;
float number=0;
num=string[0];
int i=0,decimal=1;
bool p=false;
while (string[i]!='\0'){
       num=string[i];
       if ((num=='0')||(num=='1')||(num=='2')||(num=='3')||(num=='4')||(num=='5')||(num=='6')||(num=='7')||(num=='8')||(num=='9')){
      number=number+float(atoi(num)-(atoi(num)-1));
      number=number*10;
       }else{
             p=true;
       }if (p)
             decimal++;
       }i++;
}for (int i=0;i<decimal;i++);
   div=div*10;
}number=number/div;

and there it is...

it's not the best way to do it, but it works... personally i would not recommend u 2 use it... but, if nothing else works... it's better than nothing... it could keep u out of an emergency...

thanks for that code.
But only problem is that i cannot use 2 much of library function(atoi) as it will take lot of processiing time and cycles. so i hve to shorten the code for better performance.

but, did it work?

This removes the atoi() plus it has better error handling.

For example,

  • "Hello", fails
  • "1234.567", passes
  • "678.0.345", fails
/**
 *
 * Convert a string to a float.
 *
 * @param: character array
 * @param: float passed by reference
 * @return: bool, true upon success
 */
bool string_to_float( char str[], float &number )
{
	char num;
	double div=1;
	number=0;
	
	int i=0,decimal=0;
	bool p=false;

	try
	{
		while (str[i] != '\0')
		{       
			num=str[i];       
			if (  (num=='0')
				||(num=='1')
				||(num=='2')
				||(num=='3')
				||(num=='4')
				||(num=='5')
				||(num=='6')
				||(num=='7')
				||(num=='8')
				||(num=='9')
				)
			{      
				number=number*10;       
				number=number+float(num-'0');      
			}
			else if (num=='.' && !p)
			{             
				p=true;
				i++;
				continue;
			}
			else
			{
				throw 0;
			}
			
			if (p)
			{
				decimal++;       
			}

			i++;
		}

		if ( p ) 
		{
			for (int j=0;j<decimal;j++)
			{
				div=div*10;
			}
		}
		
		number=number/div;
	}
	catch (...)
	{
		return false;
	}

	return true;
}
commented: you resurrected a year-old thread for this crap? a massive IF statement? terrible. -1

Why all of the sudden old threads are renewed ?
I made a reply to an old thread by accident & I did apologize for that. Now 2 more have been renewed.

Why all of the sudden old threads are renewed ?
I made a reply to an old thread by accident & I did apologize for that. Now 2 more have been renewed.

I think it just happens quite easily, consider a newbie who reads a thread and maybe sees something relevant/interesting in the "Similar threads" section (at the bottom of the page), clicks on a link there, and eventually posts a (more or less late) helpful reply, without thinking about when the OP posted. The keyword here is newbie, I think.

I. The keyword here is newbie, I think.

LOL! I've almost done that myself a couple of times.

Great inference!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.