there's a function in called strtof (string to float) which can perfectly do that...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
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 withi as the loop counter.
Confused now?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
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 isnewbie, I think.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
I. The keyword here is newbie, I think.
LOL! I've almost done that myself a couple of times.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343