# Help make a conversion Workflow?

## Recommended Posts

So i'm not great at the programing side of things but I think I'm figured out the math...

I'd like to create a conversion workflow that more represents what apples spotlight conversion looks like.

I've done the math but thought i'd look for help with the programming.

Currently I've only created the inches conversion but will be happy to continue once I know there is a reason too.

Attached is a mock-up & the code I've made (in no particular language, just my own problem solving language). hope someone can jump in and get me the rest of the way there.

Thanks.

```in {query}

realFeet = {query}/12     		//divide inches by 12
intFeet = Int realFeet     		//convert number to Integer (this only works if Integer conversion always rounds down.)
remainInch = {query} - (intFeet * 12)	//remaining inches in feet Imperial conversion
intRemainInch = Int remainInch		//convert number to Integer
decimal = remainInch - intRemainInch	//remaining decimals if any

If realFeet = intFeet
output = 1
elseIf decimal >= 1
output = 2
else
output = 3
if decimal = .0625
fracDem = “-1/16””
elseif decimal = .125
fracDem = “-1/8””
elseif decimal = .1875
fracDem = “-3/16””
elseif decimal = .25
fracDem = “-1/4””
elseif decimal = .3125
fracDem = “-5/16””
elseif decimal = .375
fracDem = “-3/8””
elseif decimal = .4375
fracDem = “-7/16””
elseif decimal = .5
fracDem = “-1/2””
elseif decimal = .5625
fracDem = “-9/16””
elseif decimal = .625
fracDem = “-5/8””
elseif decimal = .6875
fracDem = “-11/16””
elseif decimal = .75
fracDem = “-3/4””
elseif decimal = .8125
fracDem = “-13/16””
elseif decimal = .875
fracDem = “-7/8””
elseif decimal = .9375
fracDem = “-15/16””
else
fracDem = decimal + “ “ “	//If it's smaller then a 1/16 just use the decimal
end
end

If output = 1
Output {query} “ “ “			//Inches
Output realFeet” ’ ”			//feet
Output (realFeet/3)” yards ”		//yards
Output ({query}*25.4) “millimeters”	//millimeters
Output ({query}*2.54) “centimeters”	//centimeters
Output ({query}*.0254) “meters”		//meters
elseIf output = 2
Output {query} “ “ “			//Inches
Output realFeet” ’ ”remainInch” “ “	//feet
Output (realFeet/3)” yards ”		//yards
Output ({query}*25.4) “millimeters”	//millimeters
Output ({query}*2.54) “centimeters”	//centimeters
Output ({query}*.0254) “meters”		//meters
elseIf output = 3
Output {query} “ “ “			//Inches
Output realFeet” ’ ”remainInch fracDem	//feet
Output (realFeet/3)” yards ”		//yards
Output ({query}*25.4) “millimeters”	//millimeters
Output ({query}*2.54) “centimeters”	//centimeters
Output ({query}*.0254) “meters”		//meters

end```

Edited by natemac

I wrote this workflow that already supports more units than you can shake a stick at (thanks to the library it's based on). No real fractions, however.

I couldn't say how much interest there'd be in support for real fractions, as they're only really of interest to Americans, and USians are a (significant) minority of Alfred users according to the metrics I have.

Regardless of what you think of my workflow, I suggest you check out the pint library (which my workflow is based on) to get an idea of how best to implement unit conversion.

Your if … elseif … else … method for producing real fractions isn't realistically sustainable (you don't want such a large function for every possible unit). It needs to be abstracted into a generalised function that accepts a divisor/numerator/denominator/whatever, which can handle inches, ounces, pints, pounds, furlongs etc.

If I were you, I'd look into implementing real fractions on top of pint or a similar library. It'd save you 99% of the work. There's probably already a Python library for real fractions out there, which you could combine with pint.

Edited by deanishe

Thanks so much for the info, i'm gonna take the time and check it out... I work will carpenters all day at work, so while i'm at my computer I'm working in inches and decimals, but when I need to pass along the info I need to give them something they can use, hence the workflow. Thanks for the advice