sprintf problem

Hi

I am working on modifying some existing code (not written by me)

I am displaying an 8 digit number (actually a frequency in MHz)

i.e 14040123 should be displayed as 14,020.123 and am using the code below to do this:

void PrintFrequency(long frequency)
{
  if (frequency == 0)  // an "empty" memory channel returns frequency == 0
  { 
    LCDPrint("  Empty   ", 0, 0);
  }
  else
  {
    // this formats the frequency in the typical transceiver format with two decimal points separating MHz and KHz
    char str[11];
    sprintf(str, "%010lu", frequency); // "u" = unsigned integer

    str[0] = str[2];
    str[1] = str[3];
    str[2] = ',';
    str[3] = str[4]; 
    str[4] = str[5]; 
    str[5] = str[6]; 
    str[6] = '.';

    if (frequency < 10000000)
    {
      str[0] = ' ';  
    }

    LCDPrint(str, 0, 0);
  }
}

This works perfectly...I now wish to display the difference between two frequencies i.e. 14090000 - 14000000 = 90000 (_splitdiff)

to be formatted as +90.00(0)........... last digit not to be shown

I have worked out that "" sprintf(str, "%010lu", frequency); // "u" = unsigned integer "" must be replaced by

"" sprintf(str, "%+06li", _splitdiff); // to give a signed result 5 digits long ,"i" = signed integer, "+" makes +/- display.

but the lines that follow are a problem, I have experimented with various numbers and "." position but with no luck.

I have not seen this type of formatting method before........perhaps it is more obvious to an expert

Can you offer any help/advice please?

Regards

Steve

but the lines that follow are a problem

They are, huh? How do you know that? What problem?

Hi Paul

OK, using

void PrintSplitDiff(long _splitdiff)
{

  ClearSplitDiffArea();
  
  char str[6];
 
  sprintf(str, "%+05li", _splitdiff);

I can print out on the LCD the value of _splitdiff, eg +9999, -1234, +1234 etc

What I would like to be able to do is to display the value in the format +00.00 eg +99.99, -12.34, +12.34 etc

In the existing code a series of str[0] = str[1]; type commands after the sprintf command seem to format the printed value i.e. add "," and "." . I cant seem to find out how this is done.......example is below:

 char str[11];
    sprintf(str, "%010lu", frequency); // "u" = unsigned integer

    str[0] = str[2];
    str[1] = str[3];
    str[2] = ',';
    str[3] = str[4]; 
    str[4] = str[5]; 
    str[5] = str[6]; 
    str[6] = '.';

This formats 14020123 as 14,020.123

Any ideas?

Steve

How about creating a new variable called str_formatted(?) in order to "format" it?

The last lines of the last code you passed format the string in order to include the "." and the ",", assigning this values in positions of the string (str[n] where n=position).

If you want to do it, just be sure how many positions of the str are being used in order to not assigning values to positions you will have to use. For example:

str = 9999, and you want to show as 99.99.

You can use:
str_formatted[0] = str[0];
str_formatted[1] = str[1];
str_formatted[2] = '.';
str_formatted[3] = str[2];
str_formatted[4] = str[3];

Is it understeable?

g4edg:
What I would like to be able to do is to display the value in the format +00.00 eg +99.99, -12.34, +12.34 etc

Separate the number into two parts using div and mod operators, then print those separately into the string:

char buf[32]; // make it however big you need;
int count = 1234; // your input value
int hundreds = value/100; // hundreds contains 12
int units = value % 100; // units contains 34

snprintf(buf, sizeof(buf) "%02d:%02d", hundreds, units);
// buf now contains "12:34"