View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Not printing the right decimals - [SOLVED] |
Posted: Tue Jan 30, 2018 5:17 pm |
|
|
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 5.078 & CCS PCH 5.093
Last edited by Gabriel on Wed Jan 31, 2018 7:36 am; edited 1 time in total |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Jan 30, 2018 5:33 pm |
|
|
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 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19497
|
|
Posted: Wed Jan 31, 2018 1:40 am |
|
|
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.
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: 1067 Location: Panama
|
|
Posted: Wed Jan 31, 2018 6:45 am |
|
|
Well.. count me in as part of the people who misundertood this.
Thank you for your clear explanation! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Jan 31, 2018 7:22 am |
|
|
Nope.... this does not work
%4.xf or %5.xf or %6.xf or %7.xf
Still getting 2 decimals.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Jan 31, 2018 7:27 am |
|
|
And..... nevermind.
I seem to have forgotten how if statements work.
Sigh.... sorry about this. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|