• AVR Freaks

Problem with displaying float

Author
aslanoymak
Starting Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2012/09/12 00:57:42
  • Location: TURKEY
  • Status: offline
2013/01/10 01:59:09 (permalink)
0

Problem with displaying float

Hello to Everyone
 
İ am using pic18f4620 and mplab c18. İ am trying to display float and it is working in such values 5, 5.17 or 5.177  but when i am trying to show 5.05 it is displaying 5.5. İt doesnt show 0 if it is first after point. a few sample 
 
Real Value                       Displayed Value
4.006                                4.6
2.01                                  2.1
 
and i know why it is working like this, in mplab c18 sprintf function doenst support to float--->string conversation. and in microchip support they are offering a code so that code is working like this. İt is not working well. Here is the code below
 
void display_float_t(float x){
    char buffer[100];
    unsigned long x_dec=0;
    long x_int=0;
    x_int = (long)((float) x);
    x_dec = (long)((float)x*1000)-x_int*1000;
    sprintf(buffer,(const far rom char*)"%li.%li", x_int,x_dec);
     lcd_string( buffer);
}
when the result of the  "x_dec = (long)((float)x*1000)-x_int*1000;" is starting with 0 like 07 or 007 it is taking it like 7 and displaying 7. so if anyhow would help me to solve this i would be so glad. Thanks for your helps. 
 
 
Best Regard
 
Aslan OYMAK
 
 
 
#1

8 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:Problem with displaying float 2013/01/10 03:08:24 (permalink)
    0
    Use %03i or alike (%03u) as a format specifier, for the decimal side...

    GENOVA :D :D ! GODO
    #2
    aslanoymak
    Starting Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2012/09/12 00:57:42
    • Location: TURKEY
    • Status: offline
    Re:Problem with displaying float 2013/01/10 05:04:02 (permalink)
    +2 (1)
    thanks for replying;
     
    i tried what you offered but it didnt work. i made a fuction and it is working perfect. i am sending it now
     
    i hope it help peoples who needs that.

    Attached Image(s)

    #3
    aslanoymak
    Starting Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2012/09/12 00:57:42
    • Location: TURKEY
    • Status: offline
    Re:Problem with displaying float 2013/01/10 05:14:45 (permalink)
    0
    in this program i also made to not display "0"s after end of the decimal.
    #4
    1and0
    Access is Denied
    • Total Posts : 12095
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re:Problem with displaying float 2013/01/10 08:13:42 (permalink)
    +2 (1)
    I believe your code will fail to handle negative numbers; adding fabs() from math.h for the fractional part solves this.  Since your variable is unsigned long type, you need to use %03lu for the fractional part; %03u works for unsigned int type.  Try this:

        char buffer[100];
        long x_int=0;
        unsigned long x_dec=0;
        x_int = (long) x;
        x_dec = (long) (fabs((x - x_int)*1000) + 0.5);
        sprintf(buffer, (const far rom char*) "%li.%03lu", x_int, x_dec);

    #5
    1and0
    Access is Denied
    • Total Posts : 12095
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re:Problem with displaying float 2013/01/11 06:51:48 (permalink)
    0
    Setting aside precision and limitation, I just realized the code snippet in my previous post does not work for numbers between -1 and 0.  Searching the forum found this at Dario's CML which has the same problem, and this from Microchip's KnowledgeBase fails to handle all negative numbers.
     
    Here's a workaround provided the floating point number does not exceed the range of a unsigned long (actually 2^32 - 256):

    void ftoa(float value, char *string)
    {
        if (value < 0) {
            *string++ = '-';
            value = -value;
        }
        sprintf(string, (const far rom char *) "%lu.%03u",
                        (long) value,
                        (int) ((value - (long) value) * 1000. + 0.5));
    }

    I'm sure other algorithms are more efficient than using sprintf().
    #6
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:Problem with displaying float 2013/01/11 14:31:34 (permalink)
    0
    Yep Smile
    but with fabs(), like Ian   1And0 (sorry Smile) also suggested above, it should work... doesn't it?
    post edited by DarioG - 2013/01/12 02:30:40

    GENOVA :D :D ! GODO
    #7
    1and0
    Access is Denied
    • Total Posts : 12095
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re:Problem with displaying float 2013/01/11 15:36:04 (permalink)
    0
    DarioG
    Yep Smile
    but with fabs(), like Ian also suggested above, it should work... doesn't it?
    Ian?   It does not work because casting a negative float number between -1 and 0 to a "(long)" or "(int)" resulted in a '0' without the negative sign; e.g. -0.123 becomes "0.123".
    #8
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:Problem with displaying float 2013/01/12 02:41:22 (permalink)
    0
    Sorry, it was you - somehow I tend to give credits to Ian by default grin
    Now I get the thing about -1 .. 0 - will do some test as I happen to need it! thanks

    GENOVA :D :D ! GODO
    #9
    Jump to:
    © 2021 APG vNext Commercial Version 4.5