STR #1013: yet another attempt at fixing the precision count in Fl_Valuator. Does this work for everyone?

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4558 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Matthias Melcher 2005-09-08 20:57:27 +00:00
parent 022415c310
commit bde4916c7e

View File

@ -124,16 +124,23 @@ int Fl_Valuator::format(char* buffer) {
// Figure out how many digits are required to correctly format the
// value.
int i;
char temp[255], *ptr;
snprintf(temp, sizeof(temp), "%g", A/B);
if ((ptr = strchr(temp, '.')) != NULL)
i = strlen(ptr + 1);
else
i = 0;
int i, c = 0;
char temp[32], *ptr;
// output a number with many digits after the decimal point. This
// seems to be needed to get high precission
snprintf(temp, sizeof(temp), "%.12f", A/B);
// strip all trailing 0's
for (i=strlen(temp)-1; i>0; i--) {
if (temp[i]!='0') break;
}
// count digits until we find the decimal point (or comma or whatever
// letter is set in the current locale)
for (; i>0; i--, c++) {
if (!isdigit(temp[i])) break;
}
// MRS: THIS IS A HACK - RECOMMEND ADDING BUFFER SIZE ARGUMENT
return snprintf(buffer, 128, "%.*f", i, v);
return snprintf(buffer, 128, "%.*f", c, v);
}
//