Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Units of measurement conversion logic in C# [closed]

I am adding a feature to my program in which the user will have the ability to change their unit of measurement at any time, and have the program recalculate their input and output.

If the user inputs say, 20lbs for an item, then decides he wants to work in kilograms instead, he can select an option to do so at any time, and the program will recalculate his 20lb input to 9Kg. Then if he decides he'd rather work in ounces, it would convert that 9Kg to 320oz, so on and so forth.

What would be the most effective and efficient way to go about this? I've been racking my brain trying to figure out a way to have the correct formula be implemented.

like image 398
EvanRyan Avatar asked Nov 30 '22 10:11

EvanRyan


1 Answers

Do all your calculations in standard units (or the units you prefer to work with).

  • Convert the user's input from whatever they wish into standard units.
  • Perform the calculation using standard units.
  • Convert the answer back into the units the user wants.

Only the user interface needs to care about the unit conversions. The rest of the code does not need to worry about it.

like image 169
Mark Byers Avatar answered Dec 05 '22 01:12

Mark Byers