CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Not printing the right decimals - [SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 918
Location: Panama

View user's profile Send private message

Not printing the right decimals - [SOLVED]
PostPosted: Tue Jan 30, 2018 5:17 pm     Reply with quote

Hi Guys,

I'm trying to dynamically change the way floats are presented to a 4x20 LCD.

My intention is to sacrifice decimal places depending on the value range.
Code:

if((high_LCD<9999.99)&&(high_LCD>=1000.00)){Length=sprintf(Line_to_Print," Max:%4.0f",high_LCD);   fprintf(lcd_putc,"HIGH1\r\n");}
   if((high_LCD<999.99)&&(high_LCD>=100.00)){Length=sprintf(Line_to_Print," Max:%3.1f",high_LCD);   fprintf(lcd_putc,"HIGH2\r\n");}
   if((high_LCD<99.99)&&(high_LCD>=0.00)){Length=sprintf(Line_to_Print," Max:%3.2f",high_LCD);   fprintf(lcd_putc,"HIGH3\r\n");}
   else Length=sprintf(Line_to_Print," Max:%3.2f",high_LCD);


The above code is part of the Serial LCD driver I've made.

My problem is that regardless of how many decimals I specify i get 2 always in all 3 cases.

I should be displaying numbers as follows:
0.23
10.23
100.2
1000

(based on the range obviously)

G.
_________________
CCS PCM 4.135 - PIC: 16F886, 88', 876' 877A
CCS PCH 5.012 - PIC: 18F97j60


Last edited by Gabriel on Wed Jan 31, 2018 7:36 am; edited 1 time in total
Gabriel



Joined: 03 Aug 2009
Posts: 918
Location: Panama

View user's profile Send private message

PostPosted: Tue Jan 30, 2018 5:33 pm     Reply with quote

Well, i tested this by printing to terminal and the float format %3.xf - (x being the decimals) works but seems to fail when printing to a string.


My driver prints to a string first.... this is needed due to the way the LCD is set up because its a serial LCD and i need... blah blah blah.
_________________
CCS PCM 4.135 - PIC: 16F886, 88', 876' 877A
CCS PCH 5.012 - PIC: 18F97j60
Ttelmah



Joined: 11 Mar 2010
Posts: 12633

View user's profile Send private message

PostPosted: Wed Jan 31, 2018 1:40 am     Reply with quote

It is very important to understand what the parts of a format string mean.

%x.yf

The 'x' in C is the 'total field width' How many characters are to be allowed for the whole output value. The 'y' is the digits after the decimal.

So %3.2f, says allow total 3 characters, and this is then a decimal point and two trailing digits. Problem is that this will always overflow. 0.34, would overflow it (four characters).

If you want to print a number as xxx.x, this needs the formal %6.1f (the decimal is a counted character).

The number in front of the decimal in the format is not the digits in front of the decimal. This is a common misunderstanding in C. Very Happy

Because every one of the output layouts is overflowing, the routine switches to it's normal unformatted output.

Max:%5.0f
Max:%5.1f
Max:%5.2f

are what you need for the data sizes shown.
Gabriel



Joined: 03 Aug 2009
Posts: 918
Location: Panama

View user's profile Send private message

PostPosted: Wed Jan 31, 2018 6:45 am     Reply with quote

Well.. count me in as part of the people who misundertood this.

Thank you for your clear explanation!
_________________
CCS PCM 4.135 - PIC: 16F886, 88', 876' 877A
CCS PCH 5.012 - PIC: 18F97j60
Gabriel



Joined: 03 Aug 2009
Posts: 918
Location: Panama

View user's profile Send private message

PostPosted: Wed Jan 31, 2018 7:22 am     Reply with quote

Nope.... this does not work

%4.xf or %5.xf or %6.xf or %7.xf

Still getting 2 decimals.

G.
_________________
CCS PCM 4.135 - PIC: 16F886, 88', 876' 877A
CCS PCH 5.012 - PIC: 18F97j60
Gabriel



Joined: 03 Aug 2009
Posts: 918
Location: Panama

View user's profile Send private message

PostPosted: Wed Jan 31, 2018 7:27 am     Reply with quote

And..... nevermind.

I seem to have forgotten how if statements work.

Sigh.... sorry about this.
_________________
CCS PCM 4.135 - PIC: 16F886, 88', 876' 877A
CCS PCH 5.012 - PIC: 18F97j60
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group