Skip to content

Commit

Permalink
Round insulin value in treatment tooltip (nightscout#6776)
Browse files Browse the repository at this point in the history
* rename toFixedMin to toRoundedStr

* round the insulin value
  • Loading branch information
bassettb authored and ivalkou committed Apr 8, 2021
1 parent fdd6ca1 commit 2780927
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,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;
}
Expand All @@ -643,7 +643,7 @@ function init (client, d3) {
(treatment.protein ? '<strong>' + translate('Protein') + ':</strong> ' + treatment.protein + '<br/>' : '') +
(treatment.fat ? '<strong>' + translate('Fat') + ':</strong> ' + treatment.fat + '<br/>' : '') +
(treatment.absorptionTime > 0 ? '<strong>' + translate('Absorption Time') + ':</strong> ' + (Math.round(treatment.absorptionTime / 60.0 * 10) / 10) + 'h' + '<br/>' : '') +
(treatment.insulin ? '<strong>' + translate('Insulin') + ':</strong> ' + treatment.insulin + '<br/>' : '') +
(treatment.insulin ? '<strong>' + translate('Insulin') + ':</strong> ' + utils.toRoundedStr(treatment.insulin, 2) + '<br/>' : '') +
(treatment.enteredinsulin ? '<strong>' + translate('Combo Bolus') + ':</strong> ' + treatment.enteredinsulin + 'U, ' + treatment.splitNow + '% : ' + treatment.splitExt + '%, ' + translate('Duration') + ': ' + treatment.duration + '<br/>' : '') +
(treatment.glucose ? '<strong>' + translate('BG') + ':</strong> ' + glucose + (treatment.glucoseType ? ' (' + translate(treatment.glucoseType) + ')' : '') + '<br/>' : '') +
(treatment.enteredBy ? '<strong>' + translate('Entered By') + ':</strong> ' + treatment.enteredBy + '<br/>' : '') +
Expand Down
2 changes: 1 addition & 1 deletion lib/report_plugins/daytoday.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,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))
Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
22 changes: 11 additions & 11 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 2780927

Please sign in to comment.