-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-usable component to show quantity with significant decimal digits
* used in line analysis table * can pass the maximum number of decimal digits (which becomes the default if no uncertainty is passed) * if uncertainty is passed, both the value and uncertainty are rounded to the second significant digit in the uncertainties * might not be entirely perfect - javascript treatment of numbers can be a bit funny... but the full underlying floats are untouched via the API
- Loading branch information
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<template> | ||
<span> | ||
{{ valueTrunc }} {{ uncertainty ? "± " + uncertTrunc : null}} {{ unit }} | ||
</span> | ||
</template> | ||
|
||
<script> | ||
module.exports = { | ||
data: function () { | ||
const maxDecs = this.maxDecs || 5; | ||
var nDecs = maxDecs; | ||
var uncertTrunc = null; | ||
if (this.uncertainty !== '') { | ||
// then uncertainty was provided, so let's round both the uncertainty and value | ||
// to show up to the second significant digit in the uncertainty. | ||
nDecs = 2 - Math.log10(parseFloat(this.uncertainty)); | ||
if (nDecs > maxDecs) { | ||
nDecs = maxDecs | ||
} else if (nDecs < 0) { | ||
// NOTE: could support rounding of integers to less significant digits, but for now | ||
// we'll at least show all digits before the decimal. | ||
nDecs = 0 | ||
} | ||
uncertTrunc = +parseFloat(this.uncertainty).toFixed(nDecs) | ||
} | ||
const valueTrunc = +parseFloat(this.value).toFixed(nDecs); | ||
return { | ||
nDecs: nDecs, | ||
valueTrunc: valueTrunc, | ||
uncertTrunc: uncertTrunc | ||
} | ||
}, | ||
props: ['value', 'uncertainty', 'unit', 'maxDecs'], | ||
methods: { | ||
} | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters