My question is language independent. I want to write a simple unit convertor program. There are conversion types and according to the types there are units. For instance, if we take Length as conversion type then there'll be Meter,Inch,Foot,Kilometer,Mile and so on. Imagine there are two dropdown boxes- one's direction is from and the other's is to. And there's a textbox below each dropdown boxes. Simply when I choose Inch on the left handside and Mile on the right and type any number in either textboxes I want the equivalent to appear in the other textbox. Just like any normal unit conversion app.Now I'm very confused about this. Is just writing a long code with if...else if the only way of doing this? What way would you professionals follow?
5 Answers
Use a general calculating unit, e.g. meter. Say you use meters to calculate everything, the user chooses 'inch to foot'. Then you have to calculate the first unit into meters and meters again to mile. This way you only have to implement the scaling factors x for meter->unit (you can accomplish meter<-unit by using 1/x).
So if you have two dropdown boxes and a list of conversions meter->toUnit(unit), which converts a single meter to the corresponding value of the second unit, then you just have to use
userInput * 1/meter->toUnit(firstUnit) * meter->toUnit(secondUnit) = output
EDIT: To ensure the numerical stability I recommend you to use a integer value and a very small reference unit (for example mili- or even micrometers). See comment.

- 103,620
- 13
- 194
- 236
-
3I would add that all calculations should be done using a rational or decimal representation with arbitrary precision, rather than float (IEEE or otherwise), to avoid the slight discrepancies from representing and working with a finite-length binary representation. – Marcin Feb 13 '12 at 11:28
I would recommend using the approach described in this comment : https://stackoverflow.com/a/17548353/12526166
By using a map of conversion factor, this solves the issue of numerical instability that you may encounter when converting a variable multiple times.

- 19
- 5
You could make a list with names of units and conversion factors. Then in both dropdown boxes you select an item and use the conversion factors to calculate and the name to display in the textboxes below.
Of course you need to create one list per type (e.g. temperature, length etc).
Always use SI units internally.

- 15,025
- 28
- 93
- 119
As far as pre-selecting the results of the second listbox (only length, only weight, only volume, etc), you could set attributes on each dropdown list item. Something something like "Length","Weight","Volume", etc. That way, when you receive an event on the dropdown ("Changed", maybe?), you hide all elements in dropdown2 that don't have the same attribute as the selected item in listbox1.
You could set up a logical table. Something like:
"Mile", "Foot", 5280
"Foot", "Inch", 12
"Meter", "Milimeter", 1000
...
Then, once the items are selected in the dropdown, you find the list entry that contains both types. If conversion is from Inch -> Foot, then your factor is 12 for the result, and 1/12 for the inverse.
When the user hit's "Convert" you take the values of the selected items in the listbox and find them in your logical table. You then do the calculation into textbox2 based on the arrangement of your table.
For example, the user wishes to convert Foot -> Mile. You'd look for the table entry that contains both "Foot" and "Mile". If listbox1 is "Foot" (and "Foot" is the first item in the above list), then place listbox1.value * 5280
into listbox2. Or, if listbox1 is "Mile" (and "Mile" is the second item in the conversion list), then place listbox1.value * (1/5280)
into textbox2.
So, the placement of the original ("from") unit in the conversion table determines whether you use the conversion value or its inverse.
That's how I'd do it anyway ;)

- 731
- 1
- 7
- 15
I have already done it just to to my git hub profile and you will easily understand it Here is link https://github.com/Anuj-malviya0/unit_converter

- 23
- 6
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – 7uc1f3r Jan 20 '21 at 11:17