Skip to content

Commit

Permalink
Make rounding more consistent
Browse files Browse the repository at this point in the history
When formatting a value like 7.897 with a precision of 2 digits the code
was:
  * muliplying the value by 100 (10^precision) => 789.7;
  * rouding the value => 790;
  * dividing the value by 100 => 7.9

This is inconsistent with other float values displayed with 2 decimal
digits.  Prefer the toFixed method which returns a string, "7.90" in the
above exapmle.

Since we also have integer values, and because JS is a joke that only
knows about floats, attempt to guess if the value is an integer and do
not display decimals in this case to keep the previous behavor in this
case.
  • Loading branch information
smortex committed Dec 1, 2022
1 parent 494f89d commit f611ea7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/riemann/dash/public/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ var format = (function() {
}
precision = precision || 2;
var base = Math.pow(10, precision);
var val = Math.round(number * base) / base;
var val;
if (Math.round(number) == number)
val = number;
else
val = number.toFixed(precision);

if(!commas) {
return val;
Expand Down

0 comments on commit f611ea7

Please sign in to comment.