We have some data that is strongly bound to each other and we are using it for calculations. Its a value with a unit type and an optional relation. E.g. 1500 meter above sea level. The unit type should be replaceable (meter to foot). But the value should be converted accordingly.
I am currently thinking of two approaches:
Approach 1 saves the value in a fixed unit to the database without an conversion. The conversion would be made on the fly after loading the value with its prefered unit type. e.g. always saved in meter, but will be displayed in foot if the unit was changed + This prohibits problems from converting the value back and forth while causing rounding errors. + Calculations with the data can be done without converting it, since the saved value is always in meter - If you enter a value in foot but save it as meter and reload it, it could show not the value you entered because of a rounding error.
Approach 2 saves the value in the selected unit. But that switches the advantages with the disadvantages. And it would make it probably hard to choose a good databasetype for saving it since the unit is not always the same.
I tried to find how other people work with this kind of relation but since "unit" is a pretty strong word i can't find anything related to my problem. But I think this value/unit data structure is not so rare.
- How should I approach this kind of data?
- How should I save and load it?
- When should I convert the data?
- Am I missing some problems that could influence my decision?