Thread: printf
View Single Post
  #13   Spotlight this post!  
Unread 18-02-2007, 17:51
Redneck's Avatar
Redneck Redneck is offline
Hacker Hick
AKA: Jamie (2.0) Moran
FRC #0599 (Robodox)
Team Role: Engineer
 
Join Date: Aug 2004
Rookie Year: 2004
Location: California
Posts: 90
Redneck is just really niceRedneck is just really niceRedneck is just really niceRedneck is just really nice
Send a message via AIM to Redneck
Re: printf

Quote:
Originally Posted by Tureyhall View Post
%.3f means display a float with as many digits before the decimal as it needs, but limit it to three digits after the decimal.

%3.2f means display a float with three digits before the decimal, and two digits after the decimal.
These won't work in FRC code. The PIC18 chip used on the RC doesn't support floating point operations natively, so it's all done in software. Unfortunately, that software doesn't include the %f place holder. If you want to print a float, your only choice is to do something like this:
Code:
float variable = .... //Whatever float you're trying to print
int variabledeci;  // Integer to hold the numbers after the decimal point
variabledeci = variable - (int)variable;
variabledeci*=1000;
printf("variable = %d.%03d", (int)variable, (int)variabledeci);
So, say that variable = 3.141
variabledeci = 3.141 - (int)3.141 = 3.141 - 3 = .141
variabledeci *= 1000 = 141

If you want more decimal places, make the 1000 into 10000, 100000, etc.
__________________


Which badges can you claim?