Skip to content

Instantly share code, notes, and snippets.

@atroia
Created April 12, 2024 00:26
Show Gist options
  • Save atroia/477eab0d3107b66e64219a7795fb31e1 to your computer and use it in GitHub Desktop.
Save atroia/477eab0d3107b66e64219a7795fb31e1 to your computer and use it in GitHub Desktop.
Baseball WHIP
display dialog "Enter hits:" default answer ""
set runs to the text returned of the result
display dialog "Enter walks:" default answer ""
set walks to the text returned of the result
display dialog "Enter innings:" default answer ""
set inning to the text returned of the result
set inningMod to inning mod 1
if inningMod is equal to 0 then
set inning1 to inning
set inningDec to (inning1) as string
else
set split to theSplit(inning, ".")
set inning1 to item 1 of split as integer
set inning2 to item 2 of split as integer
if inning2 is 1 then
set in2 to "333333333333"
else if inning2 is 2 then
set in2 to "666666666666"
else if inning2 is 0 or "" then
set in2 to inning2
end if
set inningDec to (inning1 & "." & in2) as string
end if
set whip to (runs + walks) / inningDec
display dialog "Your WHIP: " & round_truncate(whip, 2)
on theSplit(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
set theArray to every text item of theString
-- restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
on round_truncate(this_number, decimal_places)
if decimal_places is 0 then
set this_number to this_number + 0.5
return number_to_text(this_number div 1)
end if
set the rounding_value to "5"
repeat decimal_places times
set the rounding_value to "0" & the rounding_value
end repeat
set the rounding_value to ("." & the rounding_value) as number
set this_number to this_number + rounding_value
9
set the mod_value to "1"
repeat decimal_places - 1 times
set the mod_value to "0" & the mod_value
end repeat
set the mod_value to ("." & the mod_value) as number
set second_part to (this_number mod 1) div the mod_value
if the length of (the second_part as text) is less than the decimal_places then
repeat decimal_places - (the length of (the second_part as text)) times
set second_part to ("0" & second_part) as string
end repeat
end if
set first_part to this_number div 1
set first_part to number_to_text(first_part)
set this_number to (first_part & "." & second_part)
return this_number
end round_truncate
on number_to_text(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment