LabWindows/CVI Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
tstanley

Engineering Notation for Formatting Functions

Status: New

Hi,

 

I know from previous discussions that CVI formatting and scanning functions are no longer going to be updated, but I thought I would suggest this anyway.  It would be nice if there was a format modifier to specify engineering notation for the formatting functions .  Note that the standard C library functions also lack this functionality, and since I don't think it would be a good idea to change the behaviour of the standard library functions, here's an opportunity to improve the CVI versions Smiley Wink.

 

Thanks.

1 Comment
JPS_CSE
Member

Here is a function I've used for years.  I hope this helps

 

//  value - in number to be formatted
// significant_digits - after the decimal point
// *formated_str - formatted string

void To_Engineering_Notation(double value, int significant_digits, char *formated_str)
    {
    int expof10;
    char sign[2];

    if(value < 0.0)
        {
        strcpy (sign, "-");
        value = -value;
        }
    else
        strcpy (sign, "");

    expof10 = (int) log10(value);
    if(expof10 > 0)
        expof10 = (expof10 / 3) * 3;
    else
        expof10 = (-expof10 + 3) / 3 * (-3);

    value *= pow(10,-expof10);

    if (value >= 1000.0)
        {
        value /= 1000.0;
        expof10 += 3;
        }
    
    if (value == 0.0)
        expof10 = 0;

    sprintf(formated_str, "%s%.*fe%d", sign, significant_digits, value, expof10);
    }