From bde4916c7e4e8a576f95a742bf362b7d55964c62 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 8 Sep 2005 20:57:27 +0000 Subject: 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 --- src/Fl_Valuator.cxx | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src') 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); } // -- cgit v1.2.3