View previous topic :: View next topic |
Author |
Message |
kometen
Joined: 12 Oct 2003 Posts: 13
|
Printf - Please help |
Posted: Tue Mar 15, 2005 12:14 pm |
|
|
I have a signed long in the range of -5000 to +5000 representing a temperature between -500,0 to +500,0 degree C
When I try to convert the value to a string using the fprintf it works fine
for values between -500,0 to -1,0. But when my value is -9 (equals -0.9)
the minus sign disappears.
Anyone have the correct printf setup to solve this problem?
Thanks in advance
Poul |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 15, 2005 2:25 pm |
|
|
Can you post a little demo program ? Then we'll just try it.
Also post your compiler version and the PIC you're using. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Mar 15, 2005 3:39 pm |
|
|
Here is what I had to do recently:
Code: |
takereadings(); //reads temperature and pressure
if (sT) // positive temp sT is a boolean giving the sign of temperature
fprintf(fish,"%%temp=+");
else // negative temp
fprintf(fish,"%%temp=-");
fprintf(fish,"%02d.%01u pres=%04lu\n\r",T,(int)dT,P); // Send data to fish
// %temp=sxx.x pres=xxxx<CRLF>
|
There ought to be a better way! _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
kometen
Joined: 12 Oct 2003 Posts: 13
|
My code |
Posted: Wed Mar 16, 2005 2:37 am |
|
|
Yes, there ought to be a better way.
I am using pcwh 3.216 .
My code:
int tanktoshow;
signed long temperature[20];
char string [20];
tanktoshow=1;
I am using this line to load the signed long 'temperature[tanktoshow]' to
sprintf(string,"%3.3Ld.%01Ld",temperature[tanktoshow]/10,temperature[tanktoshow]%100);
I want to have a readout with 1 decimal.
At values of 250, -250, 99,-99,9,-9 the minus sign works perfectly.
but at values -0.9, .. to -0.1 the minus sign is not added to the string.
Any nice solution is appreciated
best regards
Poul |
|
|
|