10-27-2015 01:23 PM
I have an app where a user inputs a value, calculations are performed, and it spits out the desired result in a Numeric control as a double variable.
Output values can range from the hundreds to the hundreds of millions.
I'd like to convert the final result to a string using the format or scan functions (easy enough), but I also want to add commas or spaces so output numbers can look like 123,456,789 or 10,234 instead of 123456789 and 10234 for the end-user's ease of recognition.
I can envision a way to do it, but it involves a lot of tedious code. Anyone have a suggestion for a simple way?
It seems some of the most basic housekeeping in CVI is its biggest time-sucker, for me anyway.
Scott in Ohio
10-28-2015 05:05 AM - edited 10-28-2015 05:05 AM
There isn't a standard function that formats with grouping so you'll have to write your own. A search on Internet returned for example this page that offers several solutions. Based on one of them I wrote this function that outputs a string formatted according to system locale:
//----------------------------------------------------------------------------------
// Function DoubleToGroupedString ()
//----------------------------------------------------------------------------------
/// HIFN DoubleToGroupedString
/// HIFN Formats a double value in a string grouping numbers according to system settings
/// HIPAR value/The value to format
/// HIPAR digits_of_precision/Number of digits after the decimal point
/// HIPAR string/The output string
/// HIRET Always 0
int DoubleToGroupedString (double value, int digits_of_precision, char *string)
{
int n, fracPart, order_of_magnitude;
double intgPart;
char decSep[8], thouSep[8];
// Get separators set in the system
GetLocaleInfoEx (LOCALE_NAME_USER_DEFAULT, LOCALE_SDECIMAL, (LPWSTR)decSep, 8);
GetLocaleInfoEx (LOCALE_NAME_USER_DEFAULT, LOCALE_STHOUSAND, (LPWSTR)thouSep, 8);
// Split number into integral and fractional part
// Pad fractional part according to user specifications
fracPart = abs(modf (value, &intgPart) * pow (10, digits_of_precision));
// Elaborate on the integral part
n = (int)intgPart;
order_of_magnitude = (n == 0) ? 1 : (int)pow (10, ((int)floor (log10 (abs(n))) / 3) * 3);
// Begin building the output string
sprintf (string, "%d", n / order_of_magnitude);
for (n = abs (n) % order_of_magnitude, order_of_magnitude /= 1000;
order_of_magnitude > 0;
n %= order_of_magnitude, order_of_magnitude /= 1000)
{
sprintf (string, "%s%s%03d", string, thouSep, abs (n / order_of_magnitude));
}
// Add fractional part
sprintf (string, "%s%s%d", string, decSep, fracPart);
// Output in the Debug Output window
DebugPrintf ("Number: %f\n", value);
DebugPrintf ("Order of magnitude: %d\n", order_of_magnitude);
DebugPrintf ("Digits of precision: %d\n", digits_of_precision);
DebugPrintf ("Formatted output: %s\n", string);
return 0;
}
GetLocaleInfoEx is part of Win32 APIs: you must include windows.h; no additional library is needed besides those included by default. The function is available both for base and full version of CVI.