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

printf statement

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



Joined: 03 Oct 2012
Posts: 200
Location: chennai

View user's profile Send private message

printf statement
PostPosted: Thu Oct 05, 2017 12:59 am     Reply with quote

I want to print the data with different decimal point based on the condition.
Below is the sample code.
Code:

// different decimal point
const float pressure_display[3][8] = {   {5.1, 5.2, 5.0, 5.2, 5.2, 5.2, 5.3, 5.2},
                              {5.0, 5.1, 5.0, 5.1, 5.1, 5.1, 5.2, 5.1},
                              {5.0, 5.1, 5.0, 5.1, 5.1, 5.1, 5.2, 5.1}
                           };

float data;

// in main loop
 data = 12345678;

// how to write the print statement to display the data with different decimal point.
printf(lcd_data, "%", data); // how to call decimal point

Please help.
Ttelmah



Joined: 11 Mar 2010
Posts: 13525

View user's profile Send private message

PostPosted: Thu Oct 05, 2017 1:47 am     Reply with quote

First thing, forget float!......

Float gives only about 6 digits of actual precision. If your number is something like 3.33 for example, it'll actually be stored as 3.32999. Not good.

Now, if your numbers were meant to be float format specifiers, they wouldn't work anyway. In C, the number in front of the decimal point is the _total field width_ (so how much space to allow for the whole output), which is then followed by the decimal and the digits to put after the decimal. So 5.1, is actually a number formatted as xxx.x five characters total.

Then format specifiers can't be variables handed to the function. You have to understand that all the layout maths is done by the compiler _before the code ever runs_. So the way to have variable decimals, would be:

Code:

void display_val(int32 number, int8 decimals)
{
    switch(decimals) {
    case 0:
       printf(lcd_data, "%9.0LW", number);
       break;
    case 1:
       printf(lcd_data, "%9.1LW", number);
       break; 
    case 2:
       printf(lcd_data, "%9.2LW", number);
       break; 
    case 3:
       printf(lcd_data, "%9.3LW", number);
       break;
    default: 
       printf(lcd_data, "%9.1LW", number);
       break;
    }
}

//then design your code to give an integer value not a float
//integer 'millivals', or 'centivals' for thousandths, hundredths etc..

   int32 data;
   
   data = 12345678;
   display_val(data,1); //will output 1234567.8
   display_val(data,2); //will output 123456.78
   //etc..
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