UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


code:rprintf

floating point print function

This is based on the origional rprintf by Pascal Stang. However numDigits now refers to digits after decimal point, and the is no '+' character printed for +ive numbers.

void rprintfFloat(char numDigits, double x)
{
	unsigned char firstplace = FALSE;
	unsigned char i=25, digit;
	double place = 1000000000000000.0;
 
	if(x<0)
	{
		rprintfChar('-');
		x=-x;							//convert to absolute value
	}
	for(;i;i--)
	{
		digit=x/place;
		if(digit | firstplace)			//digit>=1
		{
			firstplace = TRUE;			//we have reached the first digit
			rprintfChar(digit+0x30);
		}
		if(place == 1.0)
		{
			if(!firstplace)
				rprintfChar('0');	//always zero before the decimal point
			rprintfChar('.');		//the decimal point
			firstplace = TRUE;		//we have to print after the decimal place
			i=numDigits+1;
		}
		x-=digit*place;
		place/=10.0;
	}
}
code/rprintf.txt ยท Last modified: 2008/11/06 01:59 by laurenceb