Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Simple formulas to convert weights
Message
From
06/11/2015 17:42:19
 
 
To
06/11/2015 15:51:01
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Science & Medicine
Category:
Mathematics
Miscellaneous
Thread ID:
01627170
Message ID:
01627193
Views:
40
>>>>>Will be testing these now.
>>>>
>>>>Can you imagine how I look at this? Metric is so simple ...
>>>
>>>For people across the globe you should be able to use whatever measurement system they are familiar with.
>>
>>Realy? I think in that case you should stay away from a case like solution. This requires a table with the factors.
>
>Table wouldn't work. You don't have the imperial:metric case, where one would simply apply a factor (or divisor, in the other direction) and get a number. In several cases you get two numbers on the imperial side - six stone four pounds, five feet two inches, six inch and 3/8 (which they write with a minus sign between but it's not really a subtraction, it's addition). Only Celsius (of whom they never heard, they think the guy's name was Centigrade) vs Fahrenheit is simple.

But Lutz is partially correct, case/switch is not a nice solution for that, OOP should be more elegant and easier to maintain, esp if more base units come into play.

upd: after thinking a bit about how to "architect" a general conversion calculator, I'd probably go for 2 step process to convert into rational numbers for a decided upon base measure, with 2 methods (mostly for readability, as one is the inverse of the other with the same factor) for each unit: "toStandard" + "fromStandard" and the multiplicator/factor for that unit/scale as a property. Instead of the property another method to look up that value from a table (memory as in hash or disk like in SQL) could be used - although I'd probably cache the lookup access, pipe into memory hash or inject the value at compile time if using a disc based table for maintainance ease. Conversions into more than 1 scale/ not into decimal system would be programmed as a wrapper calling a sequence of such tiny conversion classesfunctions, probably via a template pattern.

Imagine having to code and maintain a conversion calculator for many units like:
http://www.convert-me.com/en/convert/weight/
and encountering a switch as basic pattern ;-))

The basic computation pattern here is a dynamic value/hash/lookup access for the base ratio, and unDRY bulldozing with a switch is...

/upd
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform