Skip to content

Commit f611ea7

Browse files
committed
Make rounding more consistent
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.
1 parent 494f89d commit f611ea7

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

lib/riemann/dash/public/format.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ var format = (function() {
66
}
77
precision = precision || 2;
88
var base = Math.pow(10, precision);
9-
var val = Math.round(number * base) / base;
9+
var val;
10+
if (Math.round(number) == number)
11+
val = number;
12+
else
13+
val = number.toFixed(precision);
1014

1115
if(!commas) {
1216
return val;

0 commit comments

Comments
 (0)