- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The kit is out of the question, jaja. I am lucky of having this PSoC in my hands. On the other hand, I tried to do what you say and did not work. The number printed is a wrong number, I've read that with float variables this can happen. Any other ideas???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Do it piece-by-piece: there are no issues in float calculations.
print the integer part of your float. Correct??
Get the fractional part. Multiply with 10, print integer part. Correct?
Multiply by 100. print integer part. Correct?
Multiply by 1000. print integer part. Correct?
Happy coding
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I had never used PSoC1 but the following works with PSoC4 and 5LP, you can convert the float into a character array using sprintf function, in your case it can be:
float var_float = 1.235456829; // This is your float
char string_to_print[10] = { 0 }; // Character array, here sprintf will place the conversion
sprintf(string_to_print, "%.2f", var_float); // The %.2f notation is what you are looking for, sprintf will take var_float and // "convert" it into a character array with 2 decimals, you want three decimals instead? use "%.3f"
LCD_PutString(string_to_print); // Print the result on the LCD
Let us know if this works, if not you can search for PSoC Latinoamerica in Facebook and there you can post questions on our beautyful mother language spanish, un saludo :).
Carlos
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@Carlos, sprintf() was not introduced for PSoC1.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Damn, i knew it was to easy to solve xD, as i said i had never used PSoC1 ( i though it was very simmilar to PSoC4 or 5 ).
To the OP: use the Bob solution 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi guys:
Well, I had to do my own method. I multiplied by 100 the float, and then I divided by 100 and stored the data in an integer, so I kept the integer part, and then I had to separate the fractional part into "one digit" pieces in order to show them on the LCD, I did it that way because otherwise I cannot show the 05 of the number 3.05 on the LCD for example, it skips the 0. So it is functioning perfectly. Thanks a lot for your help.
PS Carlos, ok, i will search for that page jajaja.
Ale.