| | |
string to float
![]() |
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
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:
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?
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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?
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.
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.
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.
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Solved Threads: 0
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. but the o\p i got is
0.122671 -0.000129 -31072
wht mistake did i perform?
thank you for ur time n suggestion.
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.
c Syntax (Toggle Plain Text)
/********************************************/ 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); } /*********************/
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...
You want to make Ancient Dragon happy?. Learn here how to properly tag the code you post.
Also read these rules about posting.
That would be a good way of saying thanks.
•
•
•
•
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.
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
why dn't you use a much simple (though longer) way to do this... this would be the "amateur way":
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...
c Syntax (Toggle Plain Text)
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;
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<-- ![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: basic I/O operation
- Next Thread: Question about including C files
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






