This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/MSP430FR2433: Issue with sprintf with unsigned int and float

Part Number: MSP430FR2433

Tool/software: TI C/C++ Compiler

Hi,

 If I use --printf_support=nofloat i'm not able to use float value. If I use minimal mode I'm not able to use unsigned integer. If I use full mode getting some memory usage error. But my requirement is to display float and unsigned interger using UART on Teraterm.

Thankyou 

  • One fairly simple trick I've used is to scale the float into an integer, then split it into Left and Right of the decimal point for formatting. Schematically, something like:

    int intval = floatval * 100;        // Save two decimal places
    int intpart = intval / 100;         // Integer part (left of decimal point)
    int decpart = intval % 100;         // Decimal digits (right)
    sprintf(str,"%d.%02d", intpart, decpart);   // Use snprintf(), not sprintf()

    I can't find any of that code, but I seem to recall a few corner cases (-0, maybe?). That's the essence, anyway.

  • I thought you were using "nofloat" rather than "minimal". I'm pretty sure "nofloat" supports field widths.
  • Line 4:
    sprintf(str,"%d.%02d", intpart, decpart); // Use snprintf(), not sprintf()
    snsprintf() is not used.

    if (decpart < 10)
    sprintf(str,"%d.0%d", intpart, decpart);
    else
    sprintf(str,"%d.%d", intpart, decpart);

    Probably less code than implementing / using modifiers.

**Attention** This is a public forum