string to float

Reply

Join Date: Apr 2007
Posts: 5
Reputation: shriagni is an unknown quantity at this point 
Solved Threads: 0
shriagni shriagni is offline Offline
Newbie Poster

string to float

 
0
  #1
Apr 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: string to float

 
0
  #2
Apr 26th, 2007
there's a function in <cstdlib> called strtof (string to float) which can perfectly do that...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string to float

 
0
  #3
Apr 26th, 2007
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

  1. char inputstr[] = "123.45";
  2. float result = 0.0F;
  3.  
  4. // convert 1st character
  5. 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:

  1. char inputstr[] = "123.45";
  2. float result = 0.0F;
  3. float temp;
  4. int nDecimalDigits;'
  5. // convert 1st character
  6. nDecimalDigits = 1; // initialize this outside the loop
  7.  
  8.  
  9.  
  10. temp = inputstr[i] - '0';
  11. nDecimalDigits *= 10;
  12. temp /= nDecimalDigits;
  13. result += temp;
  14.  

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?
Last edited by Ancient Dragon; Apr 26th, 2007 at 10:52 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: shriagni is an unknown quantity at this point 
Solved Threads: 0
shriagni shriagni is offline Offline
Newbie Poster

Re: string to float

 
0
  #4
Apr 26th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string to float

 
0
  #5
Apr 26th, 2007
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.
Last edited by Ancient Dragon; Apr 26th, 2007 at 11:12 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: shriagni is an unknown quantity at this point 
Solved Threads: 0
shriagni shriagni is offline Offline
Newbie Poster

Re: string to float

 
0
  #6
Apr 26th, 2007
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.
  1. /********************************************/
  2. main()
  3. {
  4. char inputstr[]="123.45";
  5. float result= 0.0F;
  6. float temp;
  7. int ndecimaldigit,i;
  8.  
  9. ndecimaldigit = 1;
  10.  
  11. for(i=0;i<=4;i++)
  12. {
  13. temp=inputstr[i] - '0';
  14. ndecimaldigit *= 10;
  15. temp /= ndecimaldigit;
  16. result += temp;
  17. }
  18. printf("%f %f %d \n",result,temp,ndecimaldigit);
  19. }
  20. /*********************/
but the o\p i got is
0.122671 -0.000129 -31072

wht mistake did i perform?

thank you for ur time n suggestion.
Last edited by WaltP; Apr 27th, 2007 at 1:40 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: string to float

 
0
  #7
Apr 26th, 2007
Originally Posted by shriagni View Post
dear Ancient Dragon ,
You want to make Ancient Dragon happy?. Learn here how to properly tag the code you post.

Originally Posted by shriagni View 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.
Also read these rules about posting.
That would be a good way of saying thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: string to float

 
0
  #8
Apr 27th, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: string to float

 
0
  #9
Apr 27th, 2007
why dn't you use a much simple (though longer) way to do this... this would be the "amateur way":

  1. char string[]="31.245¨;
  2. char num;
  3. double div=1;
  4. float number=0;
  5. num=string[0];
  6. int i=0,decimal=1;
  7. bool p=false;
  8. while (string[i]!='\0'){
  9. num=string[i];
  10. if ((num=='0')||(num=='1')||(num=='2')||(num=='3')||(num=='4')||(num=='5')||(num=='6')||(num=='7')||(num=='8')||(num=='9')){
  11. number=number+float(atoi(num)-(atoi(num)-1));
  12. number=number*10;
  13. }else{
  14. p=true;
  15. }if (p)
  16. decimal++;
  17. }i++;
  18. }for (int i=0;i<decimal;i++);
  19. div=div*10;
  20. }number=number/div;
  21.  
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...
Last edited by Nichito; Apr 27th, 2007 at 2:27 am.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: shriagni is an unknown quantity at this point 
Solved Threads: 0
shriagni shriagni is offline Offline
Newbie Poster

Re: string to float

 
0
  #10
Apr 27th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC