Price fraction: Difference between revisions

→‎{{header|AppleScript}}: Added a version taking a look-up table as a parameter.
(→‎{{header|AppleScript}}: Spotted type while saving.)
(→‎{{header|AppleScript}}: Added a version taking a look-up table as a parameter.)
Line 271:
Originals: 0.49, 0.79, 1.00, 0.83, 0.99, 0.23, 0.12, 0.28, 0.72, 0.37, 0.95, 0.51, 0.43, 0.52, 0.84, 0.89, 0.48, 0.48, 0.30, 0.01
Standardised: 0.62, 0.86, 1.00, 0.90, 1.00, 0.38, 0.26, 0.44, 0.82, 0.54, 0.98, 0.66, 0.58, 0.66, 0.90, 0.94, 0.62, 0.62, 0.44, 0.10"</lang>
 
An alternative that would save editing the handler in the event of the government department changing its directive would be to feed it a conversion table of up-to and standardised prices stored elsewhere.
 
<lang applescript>-- This handler just returns the standardised real value. It's up to external processes to format it for display.
 
on standardisePrice(input, table)
set integerPart to input div 1.0
set fractionalPart to input mod 1.0
if (fractionalPart is 0.0) then return input as real
repeat with thisEntry in table
if (fractionalPart ≤ beginning of thisEntry) then return integerPart + (end of thisEntry)
end repeat
end standardisePrice
 
-- Test code:
-- The conceit here is that the conversion table has been obtained from a file or from a spreadsheet application.
set table to {{0.05, 0.1}, {0.1, 0.18}, {0.15, 0.26}, {0.2, 0.32}, {0.25, 0.38}, {0.3, 0.44}, {0.35, 0.5}, {0.4, 0.54}, {0.45, 58}, {0.5, 0.62}, {0.55, 0.66}, {0.6, 0.7}, {0.65, 0.74}, {0.7, 0.78}, {0.75, 0.82}, {0.8, 0.86}, {0.85, 0.9}, {0.9, 0.94}, {0.95, 0.98}, {0.99, 1.0}}
 
set originals to {}
set standardised to {}
repeat 20 times
set price to (random number 100) / 100
set end of originals to text 2 thru -2 of ((price + 10.001) as text)
set end of standardised to text 2 thru -2 of ((standardisePrice(price, table) + 10.001) as text)
end repeat
 
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set output to linefeed & "Originals: " & originals & linefeed & "Standardised: " & standardised
set AppleScript's text item delimiters to astid
return output</lang>
 
{{output}}
<lang applescript>"
Originals: 0.92, 0.86, 0.10, 0.40, 0.00, 0.34, 0.44, 0.77, 0.67, 0.19, 1.00, 0.02, 0.49, 0.40, 0.61, 0.91, 0.85, 0.54, 0.01, 0.04
Standardised: 0.98, 0.94, 0.18, 0.54, 0.00, 0.50, 8.00, 0.86, 0.78, 0.32, 1.00, 0.10, 0.62, 0.54, 0.74, 0.98, 0.90, 0.66, 0.10, 0.10"</lang>
 
=={{header|AutoHotkey}}==
557

edits