6 ms·
The solution is actually worse than the initial problem. Unit conversion is a more complicated issue. The solution introduces all sorts of horrid conversions an
by gouranga 14y ago
The solution is actually worse than the initial problem. Unit conversion is a more complicated issue. The solution introduces all sorts of horrid conversions and rounding issues.
Consider a third of an inch. It can't be represented accurately in decimal. The same is true with other units.
You need to store in the source format which may vary based on the type of unit. As a rule, I tend to keep convertable unit values as ratios of two bignums with the unit attached as an enumeration. These can be represented and manipulated as value types (structs) in c#.