The best way to convert multiple sets of numbers with several different ratios

I am working on an open source harm reduction application for drug addicts.

One of the features of this application is the conversion (in mg / ΞΌg) between common opioids, so people do not overdose by accident.

If you are morally opposed to opioid dependence and do not respond because of your morality, please think that this application is for the HARM KEEPER .. Thus, people do not die.

I have this data.

3mg morphine IV = 10mcg fentanyl IV 2mg morphine oral = 1mg oxycodone oral 3mg oral morphine = 1mg oxymorphone oral 7.0mg morphine oral = 1mg hydromorphone oral 1mg morphine iv = .10mg oxymorphone iv 1mg morphine oral = 1mg hydrocodone oral 1mg morphine oral = 6.67mg codeine oral 1mg morphine oral = .10mg methadone oral 

And I have a text box that is the initial dosage in mg (double) that the user can enter. Under this, I have radio boxes for the source substance (i.e. morphine) and the target substance (i.e. oxycodone) for conversion.

I tried to think of the most effective way to do this, but almost everyone seems messy. If I did something like

 public static double MorphinetoOxycodone(string morphineValue) { double morphine = Double.Parse(morphineValue); return (morphine / 2 ); } 

I would also need to make a function for OxycodonetoMorphine, OxycodonetoCodeine, etc., and then dozens of functions eventually appear.

There should be an easier way than this that I am missing.

If you notice, all my transformations use morphine as their base value. What could be the easiest way to use the morphine value to convert one opioid to another? For example, if 1 mg of morphine oral is equal to 1 mg of hydrocodone, and 1 mg of morphine oral is equal to 0.10 mg of methadone, will I not multiply 1 * 10 to get the value of hydrocodone β†’ methadone? The realization of this idea is what I came across.

Any help would be greatly appreciated .. and if you would like, I would add your name / nickname to the loans in this program. It is possible that many, many people around the world will use this (I translated it into several languages), and to know that your work could help a dying drug addict. I think this is great :)

-cory

+4
source share
1 answer

I would go with an array of morphine conversion factors and the corresponding enum drugs:

 static enum Drug { Morphine, Fentanyl, Oxycodone, Oxymorphone, ... }; static double[] ratio = {1.0, 3.33, 0.5, 0.33, ... }; public static double ConvertToMorphine(Drug drug, double amount) { return amount / ratio[(int)drug]; } public static double ConvertFromMorphine(Drug drug, double amount) { return amount * ratio[(int)drug]; } public static double Convert(Drug from, Drug to, double amount) { return ConvertFromMorphine(to, ConvertToMorphine(from, amount)); } 

This way you can get any conversion you want, for example:

 Convert(Drug.Fentanyl, Drug.Oxymorphone, 5) 

This will give you the equivalent of 5 mg of fentanyl in oxymorphone.
Disclaimer: I did not code C # very well, so my syntax may be disabled.

+2
source

Source: https://habr.com/ru/post/1305430/


All Articles