diff options
| author | Matthias Melcher <fltk@matthiasm.com> | 2005-09-08 20:57:27 +0000 |
|---|---|---|
| committer | Matthias Melcher <fltk@matthiasm.com> | 2005-09-08 20:57:27 +0000 |
| commit | bde4916c7e4e8a576f95a742bf362b7d55964c62 (patch) | |
| tree | 3e030579e4e65579b7fadb7b8e2e270f4e0727cf | |
| parent | 022415c310459b0cc6940ed31661ce28aec8f0ad (diff) | |
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
| -rw-r--r-- | src/Fl_Valuator.cxx | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/Fl_Valuator.cxx b/src/Fl_Valuator.cxx index b6065a4c8..e8431f10b 100644 --- a/src/Fl_Valuator.cxx +++ b/src/Fl_Valuator.cxx @@ -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); } // |
