Project

General

Profile

Revision 445924f2

ID445924f234c4128bd9981798b2a08695c02562f6
Parent b564d570
Child d975ea31

Added by Yan-Fa Li about 10 years ago

Fix #7092: Fix number formatting for small values

humanize.filesize attempts to add byte, kb and mb to the formatting.
This isn't going to work for small values as they're not file sizes.

Write a small shim to add 'K' and 'B' and use humanize.numerFormat to
truncate the digits to 1 decimal place. Use an '_' uTemplate to sanitize the
values.

View differences:

dashboard/app/scripts/views/pgmap-view.js
1 1
/*global define*/
2 2

  
3
define(['jquery', 'underscore', 'backbone', 'templates', 'helpers/gauge-helper', 'kinetic', 'humanize', 'marionette'], function($, _, Backbone, JST, gaugeHelper, Kinetic, humanize) {
3
define(['jquery', 'underscore', 'backbone', 'templates', 'helpers/gauge-helper', 'humanize', 'kinetic', 'marionette'], function($, _, Backbone, JST, gaugeHelper, humanize, Kinetic) {
4 4
    'use strict';
5 5

  
6 6
    var PgmapView = Backbone.Marionette.ItemView.extend({
......
225 225
        },
226 226
        total: 0,
227 227
        activeclean: 0,
228
        formatNumberTemplate: _.template('<%- num %><%- unit %>'),
228 229
        format: function(v) {
229
            return humanize.filesize(v, 1000, 1).replace(' ', '').replace('b', '').toLowerCase();
230
            var num = v,
231
                unit = '',
232
                digits = 0;
233
            if (num >= 1000000) {
234
                num /= 1000000;
235
                unit = 'M';
236
                digits = 1;
237
            } else if (num >= 1000) {
238
                num /= 1000;
239
                unit = 'K';
240
                digits = 1;
241
            }
242
            return this.formatNumberTemplate({
243
                num: humanize.numberFormat(num, digits),
244
                unit: unit
245
            });
230 246
        },
231 247
        priorityOrder: [
232 248
                'stale',

Also available in: Unified diff