diff --git a/lib/client/renderer.js b/lib/client/renderer.js index a3841695060..ea5012b76b1 100644 --- a/lib/client/renderer.js +++ b/lib/client/renderer.js @@ -630,7 +630,7 @@ function init (client, d3) { var glucose = treatment.glucose; if (client.settings.units != client.ddata.profile.getUnits()) { glucose *= (client.settings.units === 'mmol' ? (1 / consts.MMOL_TO_MGDL) : consts.MMOL_TO_MGDL); - var decimals = (client.settings.units === 'mmol' ? 10 : 1); + const decimals = (client.settings.units === 'mmol' ? 10 : 1); glucose = Math.round(glucose * decimals) / decimals; } @@ -641,7 +641,7 @@ function init (client, d3) { (treatment.protein ? '' + translate('Protein') + ': ' + treatment.protein + '
' : '') + (treatment.fat ? '' + translate('Fat') + ': ' + treatment.fat + '
' : '') + (treatment.absorptionTime > 0 ? '' + translate('Absorption Time') + ': ' + (Math.round(treatment.absorptionTime / 60.0 * 10) / 10) + 'h' + '
' : '') + - (treatment.insulin ? '' + translate('Insulin') + ': ' + treatment.insulin + '
' : '') + + (treatment.insulin ? '' + translate('Insulin') + ': ' + utils.toRoundedStr(treatment.insulin, 2) + '
' : '') + (treatment.enteredinsulin ? '' + translate('Combo Bolus') + ': ' + treatment.enteredinsulin + 'U, ' + treatment.splitNow + '% : ' + treatment.splitExt + '%, ' + translate('Duration') + ': ' + treatment.duration + '
' : '') + (treatment.glucose ? '' + translate('BG') + ': ' + glucose + (treatment.glucoseType ? ' (' + translate(treatment.glucoseType) + ')' : '') + '
' : '') + (treatment.enteredBy ? '' + translate('Entered By') + ': ' + treatment.enteredBy + '
' : '') + diff --git a/lib/report_plugins/daytoday.js b/lib/report_plugins/daytoday.js index 47c16cf71a8..c8a8f7c13f7 100644 --- a/lib/report_plugins/daytoday.js +++ b/lib/report_plugins/daytoday.js @@ -762,7 +762,7 @@ daytoday.report = function report_daytoday (datastorage, sorteddaystoshow, optio } if (treatment.insulin && options.insulin) { - var dataLabel = client.utils.toFixedMin(treatment.insulin,2)+ 'U'; + var dataLabel = client.utils.toRoundedStr(treatment.insulin, 2)+ 'U'; context.append('rect') .attr('y', yInsulinScale(treatment.insulin)) .attr('height', chartHeight - yInsulinScale(treatment.insulin)) diff --git a/lib/utils.js b/lib/utils.js index 083c4284846..7d6d434472e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -34,12 +34,16 @@ function init(ctx) { } }; - utils.toFixedMin = function toFixedMin(value,digits) { + /** + * Round the number to maxDigits places, return a string + * that truncates trailing zeros + */ + utils.toRoundedStr = function toRoundedStr (value, maxDigits) { if (!value) { return '0'; } - var mult = Math.pow(10,digits); - var fixed = Math.sign(value) * Math.round(Math.abs(value)*mult) / mult; + const mult = Math.pow(10, maxDigits); + const fixed = Math.sign(value) * Math.round(Math.abs(value)*mult) / mult; if (isNaN(fixed)) return '0'; return String(fixed); }; diff --git a/tests/utils.test.js b/tests/utils.test.js index 53cccf07507..c6138f47491 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -17,18 +17,18 @@ describe('utils', function ( ) { it('format numbers short', function () { var undef; - utils.toFixedMin(3.345, 2).should.equal('3.35'); - utils.toFixedMin(5.499999999, 0).should.equal('5'); - utils.toFixedMin(5.499999999, 1).should.equal('5.5'); - utils.toFixedMin(5.499999999, 3).should.equal('5.5'); - utils.toFixedMin(123.45, -2).should.equal('100'); - utils.toFixedMin(-0.001, 2).should.equal('0'); - utils.toFixedMin(-2.47, 1).should.equal('-2.5'); - utils.toFixedMin(-2.44, 1).should.equal('-2.4'); + utils.toRoundedStr(3.345, 2).should.equal('3.35'); + utils.toRoundedStr(5.499999999, 0).should.equal('5'); + utils.toRoundedStr(5.499999999, 1).should.equal('5.5'); + utils.toRoundedStr(5.499999999, 3).should.equal('5.5'); + utils.toRoundedStr(123.45, -2).should.equal('100'); + utils.toRoundedStr(-0.001, 2).should.equal('0'); + utils.toRoundedStr(-2.47, 1).should.equal('-2.5'); + utils.toRoundedStr(-2.44, 1).should.equal('-2.4'); - utils.toFixedMin(undef, 2).should.equal('0'); - utils.toFixedMin(null, 2).should.equal('0'); - utils.toFixedMin('text', 2).should.equal('0'); + utils.toRoundedStr(undef, 2).should.equal('0'); + utils.toRoundedStr(null, 2).should.equal('0'); + utils.toRoundedStr('text', 2).should.equal('0'); }); it('merge date and time', function () {