@cryptotaxi247 / netdata-1 / commits / afb8d5804

do not strip trailing zeros when number is not decimal; fixes @1033

Costa Tsaousis (ktsaou) committed Sep 28, 2016 at 21:16 UTC afb8d580452f8f5ca0614c5cc2d2526528b6c083
1 file changed +23 -20
src/web_buffer_svg.c
+23 -20
@@ -529,7 +529,7 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
529 strcpy(value_string, "-");
530
531 else if(precision < 0) {
532 - int len, l, lstop = 0;
532 + int len, lstop = 0, trim_zeros = 1;
533
534 calculated_number abs = value;
535 if(isless(value, 0)) {
@@ -537,27 +537,30 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
537 abs = -value;
538 }
539
540 - if(isgreaterequal(abs, 1000)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf", (long double)value);
541 - else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.1Lf", (long double)value);
542 - else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.2Lf", (long double)value);
543 - else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.3Lf", (long double)value);
544 - else len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.4Lf", (long double)value);
545 -
546 - // remove trailing zeros
547 - for(l = len - 1; l > lstop ; l--) {
548 - if(likely(value_string[l] == '0')) {
549 - value_string[l] = '\0';
550 - len--;
551 - }
540 + if(isgreaterequal(abs, 1000)) { len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf", (long double)value); trim_zeros = 0; }
541 + else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.1Lf", (long double)value);
542 + else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.2Lf", (long double)value);
543 + else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.3Lf", (long double)value);
544 + else len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.4Lf", (long double)value);
545 +
546 + if(unlikely(trim_zeros)) {
547 + int l;
548 + // remove trailing zeros from the decimal part
549 + for(l = len - 1; l > lstop ; l--) {
550 + if(likely(value_string[l] == '0')) {
551 + value_string[l] = '\0';
552 + len--;
553 + }
554
553 - else if(unlikely(value_string[l] == '.')) {
554 - value_string[l] = '\0';
555 - len--;
556 - break;
557 - }
555 + else if(unlikely(value_string[l] == '.')) {
556 + value_string[l] = '\0';
557 + len--;
558 + break;
559 + }
560
559 - else
560 - break;
561 + else
562 + break;
563 + }
564 }
565
566 if(len >= 0)