He wanted each digit from the number sent by the Micro controller to be extracted separately to display on his Display Unit... While type-casting will lead to total loss of precision, and mathematical operations are useless if you think of any.
So I decided to convert this number entirely to a string, complete with the "dot" decimal and then refer to them as an array of characters. Please note that there is a loss involved due to size of the array but that is minimal.
The limit of numbers that will be displayed is 6, excluding the decimal number.
I, luckily found a library function to do the task....its called sprintf and declared inside stdio.h. Code looks like this.
/***************************************************************/
#include
#include
#include
#define nmbr 48.88 //Float number to be processed
void main()
{
clrscr();
char num[6]; //Recipient array
sprintf(num,"%f",nmbr); /*Function requires Recipient
array, Datatype float, and our
float number 48.8*/
cout<<"\nDECIMAL PART:\n";
for(int i=0;i<6;i++) //Simple array traversing
{
if(num[i]=='.')
{
cout<<"\nFRACTIONAL PART:\n";
continue;
}
cout<< num[i]; /* Instead of displaying, the
character can be processed to
obtain any desired result*/
}
getch();
}
/***************************************************************/
OUTPUT |
good stuff bhai.....:)
ReplyDelete