Metric measures to feet & inches

Im always being asked how to do this and wondered if anybady had scripted this I searched but had no luck. There are lots of converters to decimal inches for example 210cm to 82.677 but I want 6’ 11" rounded to the nearest whole inch. This is all pretty basic math but im not sure of the best way to approach this any advise or resources? a script would be preferable so I can use from Quark and copy result to clipboard and into job many thanks

Perhaps this will get you started:

set mVal to 210 as centimeters as inches as number
{getFtIn(mVal), conv(mVal)}

to getFtIn(Num)
	set ft to Num div 12
	set Ins to round (Num - ft * 12)
	return {ft, Ins}
end getFtIn

-- or this way:

to conv(Num)
	set Ins to round Num mod 12
	set ft to Num div 12
	return {ft, Ins}
end conv

Assuming I understand your question correctly, you want to convert a measurement of inches to a rounded measurement of feet and inches. Try this, where ft is the resulting number of feet and ins is the resulting number of inches.

set ft to round (total_inches / 12) rounding down
set ins to round (total_inches - ft * 12) rounding as taught in school

WF

Thank you both for your input here is what i had come up with whilst mucking about. The “rounding using rounding down” is what I didn’t know you could do tried “as integer” but this was no good. Not the most elegant way to do it.

property MyCov : 2.541
property MyFeet : 12
set UserInput to display dialog "What is your Centimetre measure" default answer "1" with icon note
try
	set CMmeasure to (text returned of UserInput) as integer
on error
	display dialog "Numbers only please!" with icon caution
end try
set MyInches to CMmeasure / MyCov
set HowManyFeet to round (MyInches / MyFeet) rounding down
set TheInches to MyInches - (HowManyFeet * MyFeet) as integer
set Output to display dialog (HowManyFeet & "'" & TheInches & """) as string

I can now build a cleaner version I think!!!

This looks a bit neater but I don’t know how to copy TheAnswer to clipboard what have I got wrong?

set UserInput to display dialog "What is your Centimetre measure" default answer "1" with icon note
try
	set CMmeasure to (text returned of UserInput) as integer
on error
	display dialog "Numbers only please!" with icon caution
end try
set Inchmeasure to CMmeasure / 2.541
set TheFeet to round (Inchmeasure / 12) rounding down
set TheInches to Inchmeasure - (TheFeet * 12) as integer
set TheAnswer to (TheFeet & "'" & TheInches & """) as string
copy it to clipboard
tell application "QuarkXPress"
	activate
	tell document 1
		paste
	end tell
end tell

Glad to be of some help. Based on Adam’s reply, I’ve just learned that Applescript has some built-in unit conversions. Interesting, considering that Applescript isn’t a very mathematical language. You can also use the Unix tool “units” in a shell script to do conversions without having to roll your own calculations. Still, I don’t think there’s a one step solution to converting one unit to a combination of two units (such as feet and inches).

WF

set UserInput to display dialog "What is your Centimetre measure" default answer "1" with icon note
try
	set CMmeasure to (text returned of UserInput) as integer
on error
	display dialog "Numbers only please!" with icon caution
end try
set Inchmeasure to CMmeasure / 2.541
set TheFeet to round (Inchmeasure / 12) rounding down
set TheInches to Inchmeasure - (TheFeet * 12) as integer
set TheAnswer to (TheFeet & "'" & TheInches & """) as string
set the clipboard to TheAnswer
tell application "QuarkXPress"
	activate
	tell document 1
		tell application "System Events"
			keystroke "v" using {command down}
		end tell
	end tell
end tell

Thank you all for some helpful tips. I will now be able to listen to my itunes in peace. An not have to quote math formula’s of yester year to the young pups that didn’t get taught this stuff at school.

Mark, the system events wasn’t needed just “paste” I thought I would have to copy something for it to go to the cilpboard but “set the clipboard to TheAnswer” was right. One more question in my line “set TheAnswer to (TheFeet & “'” & TheInches & “””) as string" how do I set the marks to straight ones like in AppleScript itself is this like (TheFeet & return & TheInches) if so what are the words I can use?

I do not have “QuarkXPress”
so I used Textedit

I assume QuarkXPress understands ‘paste’ as a command and does not think its a variable.

Here’s a variation. (The last line is untested with QuarkXPress, but works with several other apps without activation):

text returned of (display dialog "Enter the number of centimeters to convert/copy:" default answer "")
tell result as number as centimeters as inches as integer to (it div 12 as string) & "' " & it mod 12 & "\""
set the clipboard to result
tell application "QuarkXPress" to paste