mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 05:43:16 +08:00
FIX: slightly safer rounding
This commit is contained in:
parent
2f84d43bb2
commit
e66d5425e4
|
@ -8,7 +8,7 @@ registerUnbound('raw-date', dt => longDate(new Date(dt)));
|
|||
registerUnbound('age-with-tooltip', dt => new safe(autoUpdatingRelativeAge(new Date(dt), {title: true})));
|
||||
|
||||
registerUnbound('number', (orig, params) => {
|
||||
orig = parseInt(Math.round(orig), 10);
|
||||
orig = Math.round(parseFloat(orig));
|
||||
if (isNaN(orig)) { orig = 0; }
|
||||
|
||||
let title = I18n.toNumber(orig, { precision: 0 });
|
||||
|
|
|
@ -304,7 +304,7 @@ export function relativeAge(date, options) {
|
|||
export function number(val) {
|
||||
let formattedNumber;
|
||||
|
||||
val = parseInt(Math.round(val), 10);
|
||||
val = Math.round(parseFloat(val));
|
||||
if (isNaN(val)) val = 0;
|
||||
|
||||
if (val > 999999) {
|
||||
|
|
|
@ -211,10 +211,13 @@ QUnit.test("number", assert => {
|
|||
assert.equal(number(NaN), "0", "it returns 0 for NaN");
|
||||
assert.equal(number(3333), "3.3k", "it abbreviates thousands");
|
||||
assert.equal(number(2499999), "2.5M", "it abbreviates millions");
|
||||
assert.equal(number("2499999.5"), "2.5M", "it abbreviates millions");
|
||||
assert.equal(number(1000000), "1.0M", "it abbreviates a million");
|
||||
assert.equal(number(999999), "999k", "it abbreviates hundreds of thousands");
|
||||
assert.equal(number(18.2), "18", "it returns a float number rounded to an integer as a string");
|
||||
assert.equal(number(18.6), "19", "it returns a float number rounded to an integer as a string");
|
||||
assert.equal(number("12.3"), "12", "it returns a string float rounded to an integer as a string");
|
||||
assert.equal(number("12.6"), "13", "it returns a string float rounded to an integer as a string");
|
||||
});
|
||||
|
||||
QUnit.test("durationTiny", assert => {
|
||||
|
|
Loading…
Reference in New Issue
Block a user