module-builder
module-builder copied to clipboard
Truncate long decimals
Due to javascript's use of floating point numbers. But it is confusing to users and messes up graphs. Round to the nearest 1000th decimal.
Would the intended behavior be to truncate the decimal that gets saved, or to just truncate the decimal that gets displayed on the graph?
Is it possible to maintain whatever the user entered as a string, then only on export convert it to a number? Some of the percentages can get very small (see for instance the Potential_Infection state on the Sore Throat module) so I wouldn't want to confuse users more by truncating something they typed in.
We don't really have much of an 'export' concept. For the most part, what we save in the structure is what gets put into json. I believe the only exception is name on the states, which we need for internal processing but needs to get thrown out on export (which we really should clean up).
If we went that route, we would have to keep track of everything that should be a decimal but is currently stored as a string. I suppose a clever refactor could put all that logic in one place somehow, but right now that would involve having to keep track what elements are decimals in two places (within the state editor and within the export function). Maybe that isn't a very big deal.
Additionally, we would have to add this to the 'load' (or import, or whatever the opposite of export is), to change floats into truncated decimal-like strings on the way in, because otherwise this would still have the same issue for any module that the user didn't create.
It may be easier to truncate floats to decimal-like strings when rendering in the form and in the graph (like in here: https://stackoverflow.com/a/3644302). Maybe take a look at both options to see which seems cleaner?
Alternatively, there are javascript libraries that handle decimals with arbitrary length that may prevent this altogether: https://github.com/MikeMcl/bignumber.js/
A pull request has been submitted for this. Values are preserved as entered, the conversion from decimal to percentage for display in the UI was causing the long repeating decimals. The fix will keep required decimal places, not use a fixed number of decimal places. Thus 0.05 becomes "5.0%" and 0.001234 becomes "0.1234%"
That PR is #247.