Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi guys. I am doing a project for the University that deals with PSoC 1, specifically CY8C27443-24 PXI. Here is the thing, I'm trying to print a float value on a 16x2 LCD with LCD_PrString() function, this function accepts as parameter a char *. Here is an example of how I print in the LCD.

   

float var_float=1.235456829;    // this is the result of a mathematical operation, I do not know the exact value, this is an example.

   

char* var=ftoa(var_float, &status);   // this function converts from float to char* in order to show the number on the LCD

   

LCD_1_PrString(var);  // this is the function for printing on the LCD.

   

The problem is that the number displayed on the LCD has several decimals and I want to round it only in 2 or 3 decimals. The result I have in the LCD is, for example: 15,12399999999 (always with a lot of 9s at the end of the number). How Can I solved this, I've tried with several examples from internet and it does not work.

   

Thanks in advanced and sorry for my English, I am from Cuba.

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I would

   

separate fractional part from the integer part.

   

print the integer part

   

Rounding to 3 digits precision is done by (fractional part + 0.0005) * 1000 and taking the integer part.

   

Print decimal point

   

Convert integer part to string and expand with leading zeroes (0) until the length is 3

   

Print the string.

   

 

   

Or

   

Get a CY8CKIT-043 PSoC4-M Prototyping Kit and use sprintf()

   

 

   

Bob

View solution in original post

0 Likes
7 Replies