Price fraction: Difference between revisions

(Added BBC BASIC)
 
(111 intermediate revisions by 60 users not shown)
Line 1:
{{task|Financial operations}}
A friend of mine runs a pharmacy. He has a specialised function in his Dispensary application which receives a decimal value of currency and replaces it to a standard value. This value is regulated by a government department.
 
A friend of mine runs a pharmacy.   He has a specialized function in his Dispensary application which receives a decimal value of currency and replaces it to a standard value.   This value is regulated by a government department.
Task: Given a floating point value between 0.00 and 1.00, rescale according to the following table:
 
 
;Task:
Given a floating point value between   0.00   and   1.00,   rescale according to the following table:
 
>= 0.00 < 0.06 := 0.10
Line 24 ⟶ 27:
>= 0.91 < 0.96 := 0.98
>= 0.96 < 1.01 := 1.00
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F bisect_right(a, x)
V lo = 0
V hi = a.len
L lo < hi
V mid = (lo + hi) I/ 2
I x < a[mid]
hi = mid
E
lo = mid + 1
R lo
 
V _cin = [0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01]
V _cout = [0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62, 0.66, 0.70, 0.74, 0.78, 0.82, 0.86, 0.90, 0.94, 0.98, 1.00]
F pricerounder(pricein)
R :_cout[bisect_right(:_cin, pricein)]
 
L(i) 0..10
print(‘#.2 #.2’.format(i / 10, pricerounder(i / 10)))</syntaxhighlight>
 
{{out}}
<pre>
0.00 0.10
0.10 0.18
0.20 0.32
0.30 0.44
0.40 0.54
0.50 0.62
0.60 0.70
0.70 0.78
0.80 0.86
0.90 0.94
1.00 1.00
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE COUNT="20"
BYTE ARRAY levels=[6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101]
BYTE ARRAY values=[10 18 26 32 38 44 50 54 58 62 66 70 74 78 82 86 90 94 98 100]
 
PROC PrintValue(BYTE v)
PrintB(v/100) Put('.)
v=v MOD 100
PrintB(v/10)
v=v MOD 10
PrintB(v)
RETURN
 
BYTE FUNC Map(BYTE v)
BYTE i
 
FOR i=0 TO COUNT-1
DO
IF v<levels(i) THEN
RETURN (values(i))
FI
OD
RETURN (v)
 
PROC Main()
BYTE i,v
 
FOR i=0 TO 100
DO
v=Map(i)
PrintValue(v)
IF i MOD 5=4 THEN
PutE()
ELSE
Put(' )
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Price_fraction.png Screenshot from Atari 8-bit computer]
<pre>
0.10 0.10 0.10 0.10 0.10
0.10 0.18 0.18 0.18 0.18
0.18 0.26 0.26 0.26 0.26
0.26 0.32 0.32 0.32 0.32
0.32 0.38 0.38 0.38 0.38
0.38 0.44 0.44 0.44 0.44
0.44 0.50 0.50 0.50 0.50
0.50 0.54 0.54 0.54 0.54
0.54 0.58 0.58 0.58 0.58
0.58 0.62 0.62 0.62 0.62
0.62 0.66 0.66 0.66 0.66
0.66 0.70 0.70 0.70 0.70
0.70 0.74 0.74 0.74 0.74
0.74 0.78 0.78 0.78 0.78
0.78 0.82 0.82 0.82 0.82
0.82 0.86 0.86 0.86 0.86
0.86 0.90 0.90 0.90 0.90
0.90 0.94 0.94 0.94 0.94
0.94 0.98 0.98 0.98 0.98
0.98 1.00 1.00 1.00 1.00
1.00
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang Ada>
type Price is delta 0.01 digits 3 range 0.0..1.0;
function Scale (Value : Price) return Price is
Line 58 ⟶ 163:
end loop;
end Scale;
</syntaxhighlight>
</lang>
The solution uses fixed point type to prevent rounding and representation issues. With the above declarations a full coverage test:
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Price_Fraction is
Line 72 ⟶ 177:
end loop;
end Test_Price_Fraction;
</syntaxhighlight>
</lang>
{{out}}
Sample output:
<div style="height: 200px;overflow:scroll">
<pre>
Line 179 ⟶ 284:
</pre>
</div>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>; Submitted by MasterFocus --- http://tiny.cc/iTunis
 
Loop
{
InputBox, OutputVar, Price Fraction Example, Insert the value to be rounded.`n* [ 0 < value < 1 ]`n* Press ESC or Cancel to exit, , 200, 150
If ErrorLevel
Break
MsgBox % "Input: " OutputVar "`nResult: " PriceFraction( OutputVar )
}
 
;-----------------------------------------
 
PriceFraction( p_Input )
{
 
If p_Input is not float ; returns 0 if input is not a float
Return 0
 
If ( ( p_Input <= 0 ) OR ( p_Input >= 1 ) ) ; returns 0 is input is out of range
Return 0
 
; declaring the table (arbitrary delimiters in use are '§' and '|')
l_List := "0.06|0.10§0.11|0.18§0.16|0.26§0.21|0.32§0.26|0.38§0.31|0.44§0.36|0.50§0.41|0.54§0.46|0.58§0.51|0.62§0.56|0.66§0.61|0.70§0.66|0.74§0.71|0.78§0.76|0.82§0.81|0.86§0.86|0.90§0.91|0.94§0.96|0.98§1.01|1.00"
 
Loop, Parse, l_List, § ; retrieves each field (delimited by '§')
{
StringSplit, l_Array, A_LoopField, | ; splits current field (using delimiter '|')
If ( p_Input <= l_Array1 )
Return l_Array2 ; returns the second value if input <= first value
}
 
Return 0 ; returns 0, indicating failure (shouldn't be reached though)
 
}</lang>
 
=={{header|ALGOL 68}}==
Line 223 ⟶ 292:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - specimen requires formatted transput}}
<langsyntaxhighlight lang="algol68">main:
(
# Just get a random price between 0 and 1 #
Line 252 ⟶ 321:
 
printf(($"Value : "z.2dl,"Converted to standard : "z.2dl$, price, std val))
)</langsyntaxhighlight>
{{out}}
Sample Output:
<pre>
Value : 0.38
Converted to standard : 0.54
</pre>
 
=={{header|AppleScript}}==
===Procedural===
The task description doesn't make a lot of sense, implying that the pharmacist charges no more than 1.00 for his wares and that even whole-number prices are nudged by 0.10 and odd ones aren't. This offering takes any decimal currency value and standardises just the fractional part:
 
<syntaxhighlight lang="applescript">-- This handler just returns the standardised real value. It's up to external processes to format it for display.
 
on standardisePrice(input)
set integerPart to input div 1.0
set fractionalPart to input mod 1.0
if (fractionalPart is 0.0) then
return input as real
else if (fractionalPart < 0.06) then
return integerPart + 0.1
else if (fractionalPart < 0.16) then
return integerPart + 0.18 + (fractionalPart - 0.06) div 0.05 * 0.08
else if (fractionalPart < 0.36) then
return integerPart + 0.32 + (fractionalPart - 0.16) div 0.05 * 0.06
else if (fractionalPart < 0.96) then
return integerPart + 0.54 + (fractionalPart - 0.36) div 0.05 * 0.04
else
return integerPart + 1.0
end if
end standardisePrice
 
-- Test code:
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) + 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</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"
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"</syntaxhighlight>
 
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.
 
<syntaxhighlight 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</syntaxhighlight>
 
{{output}}
<syntaxhighlight 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"</syntaxhighlight>
 
===Functional===
<syntaxhighlight lang="applescript">---------------------- PRICE FRACTION ----------------------
 
property table : [¬
{0.06, 0.1}, {0.11, 0.18}, {0.16, 0.26}, {0.21, 0.32}, {0.26, 0.38}, ¬
{0.31, 0.44}, {0.36, 0.5}, {0.41, 0.54}, {0.46, 0.58}, {0.51, 0.62}, ¬
{0.56, 0.66}, {0.61, 0.7}, {0.66, 0.74}, {0.71, 0.78}, {0.76, 0.82}, ¬
{0.81, 0.86}, {0.86, 0.9}, {0.91, 0.94}, {0.96, 0.98}, {1.01, 1.0}]
 
 
-- rescaled :: [(Float, Float)] -> Float -> Float
on rescaled(table)
script
on |λ|(x)
if 0 > x or 1.01 < x then
|Left|("Out of range.")
else
|Right|(snd(my head(dropWhile(compose(ge(x), my fst), table))))
end if
end |λ|
end script
end rescaled
 
 
--------------------------- TEST ---------------------------
on run
fTable("Price adjustments:\n", ¬
showReal(2), either(identity, showReal(2)), ¬
rescaled(table), enumFromThenTo(-0.05, 0, 1.1))
end run
 
 
----------- GENERAL AND REUSABLE PURE FUNCTIONS ------------
 
-- Left :: a -> Either a b
on |Left|(x)
{type:"Either", |Left|:x, |Right|:missing value}
end |Left|
 
 
-- Right :: b -> Either a b
on |Right|(x)
{type:"Either", |Left|:missing value, |Right|:x}
end |Right|
 
 
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
on compose(f, g)
script
property mf : mReturn(f)
property mg : mReturn(g)
on |λ|(x)
mf's |λ|(mg's |λ|(x))
end |λ|
end script
end compose
 
 
-- drop :: Int -> [a] -> [a]
-- drop :: Int -> String -> String
on drop(n, xs)
set c to class of xs
if script is not c then
if string is not c then
if n < length of xs then
items (1 + n) thru -1 of xs
else
{}
end if
else
if n < length of xs then
text (1 + n) thru -1 of xs
else
""
end if
end if
else
take(n, xs) -- consumed
return xs
end if
end drop
 
 
-- dropWhile :: (a -> Bool) -> [a] -> [a]
-- dropWhile :: (Char -> Bool) -> String -> String
on dropWhile(p, xs)
set lng to length of xs
set i to 1
tell mReturn(p)
repeat while i ≤ lng and |λ|(item i of xs)
set i to i + 1
end repeat
end tell
drop(i - 1, xs)
end dropWhile
 
 
-- either :: (a -> c) -> (b -> c) -> Either a b -> c
on either(lf, rf)
script
on |λ|(e)
if missing value is |Left| of e then
tell mReturn(rf) to |λ|(|Right| of e)
else
tell mReturn(lf) to |λ|(|Left| of e)
end if
end |λ|
end script
end either
 
 
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
on enumFromThenTo(x1, x2, y)
set xs to {}
set d to x2 - x1
set v to x1
repeat until v ≥ y
set end of xs to v
set v to d + v
end repeat
return xs
end enumFromThenTo
 
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
 
-- fst :: (a, b) -> a
on fst(tpl)
if class of tpl is record then
|1| of tpl
else
item 1 of tpl
end if
end fst
 
 
-- fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
on fTable(s, xShow, fxShow, f, xs)
set ys to map(xShow, xs)
set w to maximum(map(my |length|, ys))
script arrowed
on |λ|(a, b)
justifyRight(w, space, a) & " -> " & b
end |λ|
end script
s & linefeed & unlines(zipWith(arrowed, ¬
ys, map(compose(fxShow, f), xs)))
end fTable
 
 
-- ge :: Ord a => a -> a -> Bool
on ge(a)
-- True if a is greater
-- than or equal to b.
script
on |λ|(b)
a ≥ b
end |λ|
end script
end ge
 
 
-- head :: [a] -> a
on head(xs)
if xs = {} then
missing value
else
item 1 of xs
end if
end head
 
 
-- identity :: a -> a
on identity(x)
-- The argument unchanged.
x
end identity
 
 
-- justifyLeft :: Int -> Char -> String -> String
on justifyLeft(n, cFiller, strText)
if n > length of strText then
text 1 thru n of (strText & replicate(n, cFiller))
else
strText
end if
end justifyLeft
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller, strText)
if n > length of strText then
text -n thru -1 of ((replicate(n, cFiller) as text) & strText)
else
strText
end if
end justifyRight
 
 
-- length :: [a] -> Int
on |length|(xs)
set c to class of xs
if list is c or string is c then
length of xs
else
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
end if
end |length|
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
 
 
-- maximum :: Ord a => [a] -> a
on maximum(xs)
script
on |λ|(a, b)
if a is missing value or b > a then
b
else
a
end if
end |λ|
end script
foldl(result, missing value, xs)
end maximum
 
 
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
else
x
end if
end min
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> a -> [a]
on replicate(n, a)
set out to {}
if 1 > n then return out
set dbl to {a}
repeat while (1 < n)
if 0 < (n mod 2) then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate
 
 
-- showReal :: Num b => Int -> b -> String
on showReal(n)
script
on |λ|(x)
set {l, r} to splitOn(".", (x as real) as string)
l & "." & justifyLeft(n, "0", r)
end |λ|
end script
end showReal
 
 
-- snd :: (a, b) -> b
on snd(tpl)
if class of tpl is record then
|2| of tpl
else
item 2 of tpl
end if
end snd
 
 
-- splitOn :: String -> String -> [String]
on splitOn(pat, src)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, pat}
set xs to text items of src
set my text item delimiters to dlm
return xs
end splitOn
 
 
-- str :: a -> String
on str(x)
x as text
end str
 
 
-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
set c to class of xs
if list is c then
if 0 < n then
items 1 thru min(n, length of xs) of xs
else
{}
end if
else if string is c then
if 0 < n then
text 1 thru min(n, length of xs) of xs
else
""
end if
else if script is c then
set ys to {}
repeat with i from 1 to n
set v to |λ|() of xs
if missing value is v then
return ys
else
set end of ys to v
end if
end repeat
return ys
else
missing value
end if
end take
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
 
 
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
if 1 > lng then
return {}
else
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith</syntaxhighlight>
{{Out}}
<pre>Price adjustments:
 
-0.05 -> Out of range.
0.00 -> 0.10
0.05 -> 0.10
0.10 -> 0.18
0.15 -> 0.26
0.20 -> 0.32
0.25 -> 0.38
0.30 -> 0.44
0.35 -> 0.50
0.40 -> 0.54
0.45 -> 0.58
0.50 -> 0.62
0.55 -> 0.66
0.60 -> 0.70
0.65 -> 0.74
0.70 -> 0.78
0.75 -> 0.82
0.80 -> 0.86
0.85 -> 0.90
0.90 -> 0.94
0.95 -> 0.98
1.00 -> 1.00
1.05 -> Out of range.</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">pricePoints: [
0.06 0.10 0.11 0.18 0.16 0.26 0.21 0.32
0.26 0.38 0.31 0.44 0.36 0.50 0.41 0.54
0.46 0.58 0.51 0.62 0.56 0.66 0.61 0.70
0.66 0.74 0.71 0.78 0.76 0.82 0.81 0.86
0.86 0.90 0.91 0.94 0.96 0.98 1.01 1.00
]
 
getPricePoint: function [price][
loop pricePoints [limit,correct][
if price < limit -> return correct
]
]
 
tests: [0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7849 0.9383 0.2292]
 
loop tests 'test [
print [test "=>" getPricePoint test]
]</syntaxhighlight>
 
{{out}}
 
<pre>0.3793 => 0.54
0.4425 => 0.58
0.0746 => 0.18
0.6918 => 0.78
0.2993 => 0.44
0.5486 => 0.66
0.7849 => 0.86
0.9383 => 0.98
0.2292 => 0.38</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">; Submitted by MasterFocus --- http://tiny.cc/iTunis
 
Loop
{
InputBox, OutputVar, Price Fraction Example, Insert the value to be rounded.`n* [ 0 < value < 1 ]`n* Press ESC or Cancel to exit, , 200, 150
If ErrorLevel
Break
MsgBox % "Input: " OutputVar "`nResult: " PriceFraction( OutputVar )
}
 
;-----------------------------------------
 
PriceFraction( p_Input )
{
 
If p_Input is not float ; returns 0 if input is not a float
Return 0
 
If ( ( p_Input <= 0 ) OR ( p_Input >= 1 ) ) ; returns 0 is input is out of range
Return 0
 
; declaring the table (arbitrary delimiters in use are '§' and '|')
l_List := "0.06|0.10§0.11|0.18§0.16|0.26§0.21|0.32§0.26|0.38§0.31|0.44§0.36|0.50§0.41|0.54§0.46|0.58§0.51|0.62§0.56|0.66§0.61|0.70§0.66|0.74§0.71|0.78§0.76|0.82§0.81|0.86§0.86|0.90§0.91|0.94§0.96|0.98§1.01|1.00"
 
Loop, Parse, l_List, § ; retrieves each field (delimited by '§')
{
StringSplit, l_Array, A_LoopField, | ; splits current field (using delimiter '|')
If ( p_Input <= l_Array1 )
Return l_Array2 ; returns the second value if input <= first value
}
 
Return 0 ; returns 0, indicating failure (shouldn't be reached though)
 
}</syntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
BEGIN {
O = ".06 .11 .16 .21 .26 .31 .36 .41 .46 .51 .56 .61 .66 .71 .76 .81 .86 .91 .96 1.01"
Line 282 ⟶ 930:
}
}
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
 
{{works with|QBasic}}
 
This could also be done by building an array, but I felt that this was simpler.
 
<langsyntaxhighlight lang="qbasic">DECLARE FUNCTION PriceFraction! (price AS SINGLE)
 
RANDOMIZE TIMER
Line 345 ⟶ 991:
PriceFraction! = price
END SELECT
END FUNCTION</langsyntaxhighlight>
 
Sample outputs{{out}} (run 5 times):
.7388727 .82
.8593103 .9
Line 354 ⟶ 1,000:
.0491907 .1
 
==={{header|BBC BASICBASIC256}}===
{{trans|Gambas}}
<lang bbcbasic> PRINT FNpricefraction(0.5)
<syntaxhighlight lang="basic256">arraybase 1
dim byValue = {10, 18, 26, 32, 38, 44, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 100}
dim byLimit = {6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96}
 
for byCount = 1 to 100
for byCheck = 0 to byLimit[?]
if byCount < byLimit[byCheck] then exit for
next byCheck
print ljust((byCount/100),4," "); " -> "; ljust((byValue[byCheck]/100),4," "); chr(9);
if byCount mod 5 = 0 then print
next byCount
end</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> PRINT FNpricefraction(0.5)
END
Line 378 ⟶ 1,039:
IF p < 0.91 THEN = 0.94
IF p < 0.96 THEN = 0.98
= 1.00</langsyntaxhighlight>
 
==={{header|Commodore BASIC}}===
We'll use a couple of arrays for translation. Should work for several other 8-bit BASICs after converting the screen control codes.
 
<syntaxhighlight lang="gwbasic">1 rem price fraction
2 rem rosetta code
10 data 0.06,0.1,0.11,0.18,0.16,0.26,0.21,0.32,0.26,0.38,0.31,0.44,0.36,0.5
20 data 0.41,0.54,0.46,0.58,0.51,0.62,0.56,0.66,0.61,0.70,0.66,0.74,0.71,0.78
30 data 0.76,0.82,0.81,0.86,0.86,0.90,0.91,0.94,0.96,0.98,1.01,1.0
35 rem up=user price, th=threshold, pr=price, np=new price
40 dim th(20),pr(20):th(0)=0:pr(0)=0
45 for i=1 to 20:read th(i),pr(i):next
50 print chr$(147);chr$(14);"Price Fraction":print
60 print "What is the value to calculate (between 0.0 and 1.0)";:input up
65 if up<0 or up>1.0 then goto 60
70 gosub 500
80 print:print "You entered";up;chr$(157);", the new value is";np
90 print:print "Again (Y/N)? ";
95 get k$:if k$<>"y" and k$<>"n" then 95
100 print k$
110 if k$="y" then goto 50
115 end
500 for i=0 to 20
510 if up<th(i) then np=pr(i):return
520 next i
530 np=1:return
</syntaxhighlight>
 
{{out}}
<pre>Price Fraction
 
What is the value to calculate (between 0.0 and 1.0)? 0.83344
 
You entered .83344, the new value is .9
 
Again (Y/N)? Y</pre>
 
<pre>Price Fraction
 
What is the value to calculate (between 0.0 and 1.0)? 0.05889
 
You entered .05889, the new value is .1
 
Again (Y/N)? Y</pre>
 
<pre>Price Fraction
 
What is the value to calculate (between 0.0 and 1.0)? 0.36
 
You entered .36, the new value is .54
 
Again (Y/N)? N
ready.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.050.0 Win64
 
Function rescale(price As Double) As Double
If price < 0.00 OrElse price > 1.00 Then Return price
Select Case price
Case Is < 0.06 : Return 0.10
Case Is < 0.11 : Return 0.18
Case Is < 0.16 : Return 0.26
Case Is < 0.21 : Return 0.32
Case Is < 0.26 : Return 0.38
Case Is < 0.31 : Return 0.44
Case Is < 0.36 : Return 0.50
Case Is < 0.41 : Return 0.54
Case Is < 0.46 : Return 0.58
Case Is < 0.51 : Return 0.62
Case Is < 0.56 : Return 0.66
Case Is < 0.61 : Return 0.70
Case Is < 0.66 : Return 0.74
Case Is < 0.71 : Return 0.78
Case Is < 0.76 : Return 0.82
Case Is < 0.81 : Return 0.86
Case Is < 0.86 : Return 0.90
Case Is < 0.91 : Return 0.94
Case Is < 0.96 : Return 0.98
End Select
Return 1.00
End Function
 
For i As Integer = 1 To 100
Dim d As Double = i/100.0
Print Using "#.##"; d;
Print " -> ";
Print Using "#.##"; rescale(d);
Print " ";
If i Mod 5 = 0 Then Print
Next
 
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
0.01 -> 0.10 0.02 -> 0.10 0.03 -> 0.10 0.04 -> 0.10 0.05 -> 0.10
0.06 -> 0.18 0.07 -> 0.18 0.08 -> 0.18 0.09 -> 0.18 0.10 -> 0.18
0.11 -> 0.26 0.12 -> 0.26 0.13 -> 0.26 0.14 -> 0.26 0.15 -> 0.26
0.16 -> 0.32 0.17 -> 0.32 0.18 -> 0.32 0.19 -> 0.32 0.20 -> 0.32
0.21 -> 0.38 0.22 -> 0.38 0.23 -> 0.38 0.24 -> 0.38 0.25 -> 0.38
0.26 -> 0.44 0.27 -> 0.44 0.28 -> 0.44 0.29 -> 0.44 0.30 -> 0.44
0.31 -> 0.50 0.32 -> 0.50 0.33 -> 0.50 0.34 -> 0.50 0.35 -> 0.50
0.36 -> 0.54 0.37 -> 0.54 0.38 -> 0.54 0.39 -> 0.54 0.40 -> 0.54
0.41 -> 0.58 0.42 -> 0.58 0.43 -> 0.58 0.44 -> 0.58 0.45 -> 0.58
0.46 -> 0.62 0.47 -> 0.62 0.48 -> 0.62 0.49 -> 0.62 0.50 -> 0.62
0.51 -> 0.66 0.52 -> 0.66 0.53 -> 0.66 0.54 -> 0.66 0.55 -> 0.66
0.56 -> 0.70 0.57 -> 0.70 0.58 -> 0.70 0.59 -> 0.70 0.60 -> 0.70
0.61 -> 0.74 0.62 -> 0.74 0.63 -> 0.74 0.64 -> 0.74 0.65 -> 0.74
0.66 -> 0.78 0.67 -> 0.78 0.68 -> 0.78 0.69 -> 0.78 0.70 -> 0.78
0.71 -> 0.82 0.72 -> 0.82 0.73 -> 0.82 0.74 -> 0.82 0.75 -> 0.82
0.76 -> 0.86 0.77 -> 0.86 0.78 -> 0.86 0.79 -> 0.86 0.80 -> 0.86
0.81 -> 0.90 0.82 -> 0.90 0.83 -> 0.90 0.84 -> 0.90 0.85 -> 0.90
0.86 -> 0.94 0.87 -> 0.94 0.88 -> 0.94 0.89 -> 0.94 0.90 -> 0.94
0.91 -> 0.98 0.92 -> 0.98 0.93 -> 0.98 0.94 -> 0.98 0.95 -> 0.98
0.96 -> 1.00 0.97 -> 1.00 0.98 -> 1.00 0.99 -> 1.00 1.00 -> 1.00
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=87527eed297164593d88aa2c35898eaf Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim byValue As Byte[] = [10, 18, 26, 32, 38, 44, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 100]
Dim byLimit As Byte[] = [6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96]
Dim byCount, byCheck As Byte
 
For byCount = 0 To 100
For byCheck = 0 To byLimit.Max
If byCount < byLimit[byCheck] Then Break
Next
Print Format(byCount / 100, "0.00") & " = " & Format(byValue[byCheck] / 100, "0.00") & gb.Tab;
If byCount Mod 5 = 0 Then Print
Next
 
End </syntaxhighlight>
Output:
<pre>
0.00 = 0.10
0.01 = 0.10 0.02 = 0.10 0.03 = 0.10 0.04 = 0.10 0.05 = 0.10
0.06 = 0.18 0.07 = 0.18 0.08 = 0.18 0.09 = 0.18 0.10 = 0.18
0.11 = 0.26 0.12 = 0.26 0.13 = 0.26 0.14 = 0.26 0.15 = 0.26
0.16 = 0.32 0.17 = 0.32 0.18 = 0.32 0.19 = 0.32 0.20 = 0.32
0.21 = 0.38 0.22 = 0.38 0.23 = 0.38 0.24 = 0.38 0.25 = 0.38
0.26 = 0.44 0.27 = 0.44 0.28 = 0.44 0.29 = 0.44 0.30 = 0.44
0.31 = 0.50 0.32 = 0.50 0.33 = 0.50 0.34 = 0.50 0.35 = 0.50
0.36 = 0.54 0.37 = 0.54 0.38 = 0.54 0.39 = 0.54 0.40 = 0.54
0.41 = 0.58 0.42 = 0.58 0.43 = 0.58 0.44 = 0.58 0.45 = 0.58
0.46 = 0.62 0.47 = 0.62 0.48 = 0.62 0.49 = 0.62 0.50 = 0.62
0.51 = 0.66 0.52 = 0.66 0.53 = 0.66 0.54 = 0.66 0.55 = 0.66
0.56 = 0.70 0.57 = 0.70 0.58 = 0.70 0.59 = 0.70 0.60 = 0.70
0.61 = 0.74 0.62 = 0.74 0.63 = 0.74 0.64 = 0.74 0.65 = 0.74
0.66 = 0.78 0.67 = 0.78 0.68 = 0.78 0.69 = 0.78 0.70 = 0.78
0.71 = 0.82 0.72 = 0.82 0.73 = 0.82 0.74 = 0.82 0.75 = 0.82
0.76 = 0.86 0.77 = 0.86 0.78 = 0.86 0.79 = 0.86 0.80 = 0.86
0.81 = 0.90 0.82 = 0.90 0.83 = 0.90 0.84 = 0.90 0.85 = 0.90
0.86 = 0.94 0.87 = 0.94 0.88 = 0.94 0.89 = 0.94 0.90 = 0.94
0.91 = 0.98 0.92 = 0.98 0.93 = 0.98 0.94 = 0.98 0.95 = 0.98
0.96 = 1.00 0.97 = 1.00 0.98 = 1.00 0.99 = 1.00 1.00 = 1.00
</pre>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">
dim DR(38) 'decimal range
dim PF(38) 'corresponding price fraction
range$="0.06 0.11 0.16 0.21 0.26 0.31 0.36 0.41 0.46 0.51 0.56 0.61 0.66 0.71 0.76 0.81 0.86 0.91 0.96 0.01"
frac$="0.10 0.18 0.26 0.32 0.38 0.44 0.50 0.54 0.58 0.62 0.66 0.70 0.74 0.78 0.82 0.86 0.90 0.94 0.98 1.00"
for i = 1 to 38
DR(i)=val(word$(range$,i))
PF(i)=val(word$(frac$,i))
next
 
for i = 0 to .99 step 0.03
print i;" -> ";PriceFraction(i)
next
end
 
Function PriceFraction(n)
PriceFraction=n 'return original if outside test bounds
for i = 1 to 38
if n<=DR(i) then
PriceFraction=PF(i)
exit for
end if
next
end function
</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.f PriceFraction(price.f)
;returns price unchanged if value is invalid
Protected fraction
Select price * 100
Case 0 To 5
fraction = 10
Case 06 To 10
fraction = 18
Case 11 To 15
fraction = 26
Case 16 To 20
fraction = 32
Case 21 To 25
fraction = 38
Case 26 To 30
fraction = 44
Case 31 To 35
fraction = 5
Case 36 To 40
fraction = 54
Case 41 To 45
fraction = 58
Case 46 To 50
fraction = 62
Case 51 To 55
fraction = 66
Case 56 To 60
fraction = 7
Case 61 To 65
fraction = 74
Case 66 To 70
fraction = 78
Case 71 To 75
fraction = 82
Case 76 To 80
fraction = 86
Case 81 To 85
fraction = 9
Case 86 To 90
fraction = 94
Case 91 To 95
fraction = 98
Case 96 To 100
fraction = 100
Default
ProcedureReturn price
EndSelect
ProcedureReturn fraction / 100
EndProcedure
 
If OpenConsole()
Define x.f, i
For i = 1 To 10
x = Random(10000)/10000
PrintN(StrF(x, 4) + " -> " + StrF(PriceFraction(x), 2))
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>0.3793 -> 0.54
0.4425 -> 0.58
0.0746 -> 0.18
0.6918 -> 0.78
0.2993 -> 0.44
0.5486 -> 0.66
0.7848 -> 0.86
0.9383 -> 0.98
0.2292 -> 0.38
0.9560 -> 1.00</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">data .06, .1,.11,.18,.16,.26,.21,.32,.26,.38,.31,.44,.36,.50,.41,.54,.46,.58,.51,.62
data .56,.66,.61,.70,.66,.74,.71,.78,.76,.82,.81,.86,.86,.90,.91,.94,.96,.98
 
dim od(100)
dim nd(100)
for i = 1 to 19
read oldDec
read newDec
j = j + 1
for j = j to oldDec * 100
nd(j) = newDec
next j
next i
 
[loop]
input "Gimme a number";numb
decm = val(using("##",(numb mod 1) * 100))
print numb;" -->";nd(decm)
 
goto [loop]</syntaxhighlight>
<pre>Gimme a number?12.676
12.676 -->0.78
Gimme a number?4.876
4.876 -->0.94
Gimme a number?34.12
34.12 -->0.26</pre>
 
==={{header|True BASIC}}===
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">FUNCTION pricefraction(price)
!returns price unchanged if invalid value
SELECT CASE price
CASE IS < 0
LET pricefraction = price
CASE IS < .06
LET pricefraction = .1
CASE IS < .11
LET pricefraction = .18
CASE IS < .16
LET pricefraction = .26
CASE IS < .21
LET pricefraction = .32
CASE IS < .26
LET pricefraction = .38
CASE IS < .31
LET pricefraction = .44
CASE IS < .36
LET pricefraction = .5
CASE IS < .41
LET pricefraction = .54
CASE IS < .46
LET pricefraction = .58
CASE IS < .51
LET pricefraction = .62
CASE IS < .56
LET pricefraction = .66
CASE IS < .61
LET pricefraction = .7
CASE IS < .66
LET pricefraction = .74
CASE IS < .71
LET pricefraction = .78
CASE IS < .76
LET pricefraction = .82
CASE IS < .81
LET pricefraction = .86
CASE IS < .86
LET pricefraction = .9
CASE IS < .91
LET pricefraction = .94
CASE IS < .96
LET pricefraction = .98
CASE IS < 1.01
LET pricefraction = 1
CASE ELSE
LET pricefraction = price
END SELECT
END FUNCTION
 
RANDOMIZE
FOR i = 1 TO 100
LET d = RND
PRINT USING "#.##": d;
PRINT " -> ";
PRINT USING "#.## ": pricefraction(d);
IF REMAINDER(i,5) = 0 THEN PRINT
NEXT i
END</syntaxhighlight>
 
==={{header|uBasic/4tH}}===
{{trans|Forth}}
{{works with|R3}}
<syntaxhighlight lang="text">For i = 0 To 100 Step 5
Print Using "+?.##"; i, Using "+?.##"; FUNC(_Normalize (FUNC(_Classify (i))))
Next
 
End
 
_Normalize ' normalize the price
Param (1) ' class
Local (4) ' accumulator, increment, switch and iterator
b@ = 0 : c@ = 10 : d@ = 2 ' setup accumulator, increment and switch
For e@ = 0 to a@ ' from zero to class
If And(e@ + 1, d@) Then d@ = And(d@ + d@, 15) : c@ = c@ - 2
b@ = b@ + c@ ' switch increment if needed
Next ' accumulate price
Return (Min(b@, 100)) ' clip top of price in accumulator
' calculate class
_Classify Param (1) : Return ((a@ - (a@>0)) / 5)</syntaxhighlight>
Output:
<pre>
0.00 0.10
0.05 0.10
0.10 0.18
0.15 0.26
0.20 0.32
0.25 0.38
0.30 0.44
0.35 0.50
0.40 0.54
0.45 0.58
0.50 0.62
0.55 0.66
0.60 0.70
0.65 0.74
0.70 0.78
0.75 0.82
0.80 0.86
0.85 0.90
0.90 0.94
0.95 0.98
1.00 1.00
 
0 OK, 0:115</pre>
 
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">
Option Explicit
 
Sub Main()
Dim test, i As Long
test = Array(0.34, 0.070145, 0.06, 0.05, 0.50214, 0.56, 1#, 0.99, 0#, 0.7388727)
For i = 0 To UBound(test)
Debug.Print test(i) & " := " & Price_Fraction(CSng(test(i)))
Next i
End Sub
 
Private Function Price_Fraction(n As Single) As Single
Dim Vin, Vout, i As Long
Vin = Array(0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01)
Vout = Array(0.1, 0.18, 0.26, 0.32, 0.38, 0.44, 0.5, 0.54, 0.58, 0.62, 0.66, 0.7, 0.74, 0.78, 0.82, 0.86, 0.9, 0.94, 0.98, 1#)
For i = 0 To UBound(Vin)
If n < Vin(i) Then Price_Fraction = Vout(i): Exit For
Next i
End Function</syntaxhighlight>
{{Out}}
<pre>0.34 := 0.5
0.070145 := 0.18
0.06 := 0.18
0.05 := 0.1
0.50214 := 0.62
0.56 := 0.7
1 := 1
0.99 := 1
0 := 0.1
0.7388727 := 0.82</pre>
 
==={{header|Yabasic}}===
{{trans|BASIC256}}
<syntaxhighlight lang="vb">data 10, 18, 26, 32, 38, 44, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 100
data 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96
 
dim od(21)
for i = 1 to 20
read oldDec
od(i) = oldDec
next i
dim nd(20)
for j = 1 to 19
read newDec
nd(j) = newDec
next j
 
for i = 1 to 100
for j = 1 to arraysize(nd(),1)-1
if i < nd(j) break
next j
print (i/100) using ("#.##"), " -> ", (od(j)/100) using ("#.##"), "\t";
if mod(i, 5) = 0 print
next i
end</syntaxhighlight>
 
=={{header|Beads}}==
<syntaxhighlight lang="beads">beads 1 program 'Price fraction'
 
record a_table
value
rescaled
const table : array of a_table = [<
value, rescaled
0.06, 0.10
0.11, 0.18
0.16, 0.26
0.21, 0.32
0.26, 0.38
0.31, 0.44
0.36, 0.50
0.41, 0.54
0.46, 0.58
0.51, 0.62
0.56, 0.66
0.61, 0.70
0.66, 0.74
0.71, 0.78
0.76, 0.82
0.81, 0.86
0.86, 0.90
0.91, 0.94
0.96, 0.98
1.01, 1.00 >]
 
const a_test = [0.05 0.62 0.34 0.93 0.45]
 
calc main_init
loop across:a_test val:v
loop across:table index:ix
if v < table[ix].value
log "{v} => {table[ix].rescaled}"
exit</syntaxhighlight>
{{out}}
<pre>0.05 => 0.1
0.62 => 0.74
0.34 => 0.5
0.93 => 0.98
0.45 => 0.58</pre>
 
 
=={{header|Bracmat}}==
Bracmat has no native support for floating point variables nor for the fixed point values in the conversion table. Instead thisThis solution just applies a string comparison.
<langsyntaxhighlight lang="bracmat">( ( convert
=
. ("0.06"."0.10")
Line 418 ⟶ 1,584:
& out$(!a "-->" convert$!a)
)
)</langsyntaxhighlight>
{{out}}
Output:
<pre>0.00 --> 0.10
0.01 --> 0.10
Line 459 ⟶ 1,625:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
 
double table[][2] = {
Line 486 ⟶ 1,652:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">namespace ConsoleApplication1
{
class Program
Line 541 ⟶ 1,707:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 574 ⟶ 1,740:
return 0 ;
}
</syntaxhighlight>
</lang>
 
{{out}}
<pre>Sample output:
<pre>
Enter a fractional number between 0 and 1 ( 0 to end )!
0.7
Line 591 ⟶ 1,758:
 
=={{header|Clipper}}==
<langsyntaxhighlight lang="dbase">FUNCTION PriceFraction( npQuantDispensed )
LOCAL aPriceFraction := { {0,.06,.1},;
{.06,.11,.18}, ;
Line 623 ⟶ 1,790:
nResult := aPriceFraction[ nScan ][ 3 ]
END IF
RETURN nResult</langsyntaxhighlight>
 
The function above crashes with an array access bound error if the value passed is negative.
Line 629 ⟶ 1,796:
The following is a more concise solution:
 
<langsyntaxhighlight Clipperlang="clipper">Procedure Main()
Local i
For i := -0.02 to 1.02 STEP 0.03
Line 672 ⟶ 1,839:
nResult := NIL
Endif
Return nResult</langsyntaxhighlight>
 
Output:
 
{{out}}
<pre style="height:30ex;overflow:scroll"> -0.02 -> NIL 0.00 -> 0.10
0.01 -> 0.10 0.03 -> 0.10
Line 711 ⟶ 1,877:
0.97 -> 1.00 0.99 -> 1.00
1.00 -> 1.00 1.02 -> NIL</pre>
 
=={{header|Clojure}}==
{{trans|JavaScript}}
<syntaxhighlight lang="clojure">(def values [10 18 26 32 38 44 50 54 58 62 66 70 74 78 82 86 90 94 98 100])
 
(defn price [v]
(format "%.2f" (double (/ (values (int (/ (- (* v 100) 1) 5))) 100))))</syntaxhighlight>
 
 
 
 
 
{{out}}
<pre>
user=> (price 0.50)
"0.62"
user=> (let [k (map #(double (/ % 100)) (range 101))] (sort (zipmap k (map #(price %) k))))
([0.0 "0.10"] [0.01 "0.10"] [0.02 "0.10"] [0.03 "0.10"] [0.04 "0.10"] [0.05 "0.10"]
[0.06 "0.18"] [0.07 "0.18"] [0.08 "0.18"] [0.09 "0.18"] [0.1 "0.18"]
[0.11 "0.26"] [0.12 "0.26"] [0.13 "0.26"] [0.14 "0.26"] [0.15 "0.26"]
[0.16 "0.32"] [0.17 "0.32"] [0.18 "0.32"] [0.19 "0.32"] [0.2 "0.32"]
[0.21 "0.38"] [0.22 "0.38"] [0.23 "0.38"] [0.24 "0.38"] [0.25 "0.38"]
[0.26 "0.44"] [0.27 "0.44"] [0.28 "0.44"] [0.29 "0.44"] [0.3 "0.44"]
[0.31 "0.50"] [0.32 "0.50"] [0.33 "0.50"] [0.34 "0.50"] [0.35 "0.50"]
[0.36 "0.54"] [0.37 "0.54"] [0.38 "0.54"] [0.39 "0.54"] [0.4 "0.54"]
[0.41 "0.58"] [0.42 "0.58"] [0.43 "0.58"] [0.44 "0.58"] [0.45 "0.58"]
[0.46 "0.62"] [0.47 "0.62"] [0.48 "0.62"] [0.49 "0.62"] [0.5 "0.62"]
[0.51 "0.66"] [0.52 "0.66"] [0.53 "0.66"] [0.54 "0.66"] [0.55 "0.66"]
[0.56 "0.70"] [0.57 "0.70"] [0.58 "0.70"] [0.59 "0.70"] [0.6 "0.70"]
[0.61 "0.74"] [0.62 "0.74"] [0.63 "0.74"] [0.64 "0.74"] [0.65 "0.74"]
[0.66 "0.78"] [0.67 "0.78"] [0.68 "0.78"] [0.69 "0.78"] [0.7 "0.78"]
[0.71 "0.82"] [0.72 "0.82"] [0.73 "0.82"] [0.74 "0.82"] [0.75 "0.82"]
[0.76 "0.86"] [0.77 "0.86"] [0.78 "0.86"] [0.79 "0.86"] [0.8 "0.86"]
[0.81 "0.90"] [0.82 "0.90"] [0.83 "0.90"] [0.84 "0.90"] [0.85 "0.90"]
[0.86 "0.94"] [0.87 "0.94"] [0.88 "0.94"] [0.89 "0.94"] [0.9 "0.94"]
[0.91 "0.98"] [0.92 "0.98"] [0.93 "0.98"] [0.94 "0.98"] [0.95 "0.98"]
[0.96 "1.00"] [0.97 "1.00"] [0.98 "1.00"] [0.99 "1.00"] [1.0 "1.00"])</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(defun scale (value)
(cond ((minusp value) (error "invalid value: ~A" value))
((< value 0.06) 0.10)
((< value 0.11) 0.18)
((< value 0.16) 0.26)
((< value 0.21) 0.32)
((< value 0.26) 0.38)
((< value 0.31) 0.44)
((< value 0.36) 0.50)
((< value 0.41) 0.54)
((< value 0.46) 0.58)
((< value 0.51) 0.62)
((< value 0.56) 0.66)
((< value 0.61) 0.70)
((< value 0.66) 0.74)
((< value 0.71) 0.78)
((< value 0.76) 0.82)
((< value 0.81) 0.86)
((< value 0.86) 0.90)
((< value 0.91) 0.94)
((< value 0.96) 0.98)
((< value 1.01) 1.00)
(t (error "invalid value: ~A" value))))</syntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.range;
 
double priceRounder(in double price) pure nothrow
Line 719 ⟶ 1,947:
assert(price >= 0 && price <= 1.0);
} body {
enumstatic immutable cin = [.06, .11, .16, .21, .26, .31, .36, .41, .46, .51,
.56, .61, .6646, .7151, .7656, .8161, .8666, .9171, .9676, 1.01];81,
enum cout = [.10, .18, .26, .32, .38, .44, .5086, .5491, .5896, 1.6201],
.66, .70, cout = [.7410, .7818, .8226, .8632, .9038, .9444, .9850, 1.00];54,
.58, .62, .66, .70, .74, .78, .82, .86,
foreach (i, p; cin)
.90, .94, .98, 1.00];
if (p >= price)
return cout[icin.assumeSorted.lowerBound(price).length];
assert(0);
}
 
void main() {
foreach (const price; [0.7388727, 0.8593103, 0.826687, 0.3444635])
writeln(priceRounder(price)).priceRounder.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>0.82
Line 738 ⟶ 1,965:
0.9
0.5</pre>
 
=={{header|Dart}}==
{{trans|Swift}}
<syntaxhighlight lang="Dart">
class Range {
final double start;
final double end;
 
Range(this.start, this.end);
 
bool contains(double value) {
return value >= start && value < end;
}
}
 
List<MapEntry<Range, double>> ranges = [
MapEntry(Range(0.00, 0.06), 0.10),
MapEntry(Range(0.06, 0.11), 0.18),
MapEntry(Range(0.11, 0.16), 0.26),
MapEntry(Range(0.16, 0.21), 0.32),
MapEntry(Range(0.21, 0.26), 0.38),
MapEntry(Range(0.26, 0.31), 0.44),
MapEntry(Range(0.31, 0.36), 0.50),
MapEntry(Range(0.36, 0.41), 0.54),
MapEntry(Range(0.41, 0.46), 0.58),
MapEntry(Range(0.46, 0.51), 0.62),
MapEntry(Range(0.51, 0.56), 0.66),
MapEntry(Range(0.56, 0.61), 0.70),
MapEntry(Range(0.61, 0.66), 0.74),
MapEntry(Range(0.66, 0.71), 0.78),
MapEntry(Range(0.71, 0.76), 0.82),
MapEntry(Range(0.76, 0.81), 0.86),
MapEntry(Range(0.81, 0.86), 0.90),
MapEntry(Range(0.86, 0.91), 0.94),
MapEntry(Range(0.91, 0.96), 0.98),
MapEntry(Range(0.96, 1.01), 1.00),
];
 
double adjustDouble(double val, List<MapEntry<Range, double>> ranges) {
for (var range in ranges) {
if (range.key.contains(val)) {
return range.value;
}
}
return val; // Return the original value if no range is found
}
 
void main() {
for (double val = 0.0; val <= 1.0; val += 0.01) {
String strFmt(double n) => n.toStringAsFixed(2);
 
double adjusted = adjustDouble(val, ranges);
print("${strFmt(val)} -> ${strFmt(adjusted)}");
}
}
</syntaxhighlight>
{{out}}
<pre style="overflow: scroll; height: 20em">
0.00 -> 0.10
0.01 -> 0.10
0.02 -> 0.10
0.03 -> 0.10
0.04 -> 0.10
0.05 -> 0.10
0.06 -> 0.18
0.07 -> 0.18
0.08 -> 0.18
0.09 -> 0.18
0.10 -> 0.18
0.11 -> 0.18
0.12 -> 0.26
0.13 -> 0.26
0.14 -> 0.26
0.15 -> 0.26
0.16 -> 0.32
0.17 -> 0.32
0.18 -> 0.32
0.19 -> 0.32
0.20 -> 0.32
0.21 -> 0.38
0.22 -> 0.38
0.23 -> 0.38
0.24 -> 0.38
0.25 -> 0.38
0.26 -> 0.44
0.27 -> 0.44
0.28 -> 0.44
0.29 -> 0.44
0.30 -> 0.44
0.31 -> 0.50
0.32 -> 0.50
0.33 -> 0.50
0.34 -> 0.50
0.35 -> 0.50
0.36 -> 0.54
0.37 -> 0.54
0.38 -> 0.54
0.39 -> 0.54
0.40 -> 0.54
0.41 -> 0.58
0.42 -> 0.58
0.43 -> 0.58
0.44 -> 0.58
0.45 -> 0.58
0.46 -> 0.62
0.47 -> 0.62
0.48 -> 0.62
0.49 -> 0.62
0.50 -> 0.62
0.51 -> 0.66
0.52 -> 0.66
0.53 -> 0.66
0.54 -> 0.66
0.55 -> 0.66
0.56 -> 0.70
0.57 -> 0.70
0.58 -> 0.70
0.59 -> 0.70
0.60 -> 0.70
0.61 -> 0.74
0.62 -> 0.74
0.63 -> 0.74
0.64 -> 0.74
0.65 -> 0.74
0.66 -> 0.78
0.67 -> 0.78
0.68 -> 0.78
0.69 -> 0.78
0.70 -> 0.78
0.71 -> 0.82
0.72 -> 0.82
0.73 -> 0.82
0.74 -> 0.82
0.75 -> 0.82
0.76 -> 0.86
0.77 -> 0.86
0.78 -> 0.86
0.79 -> 0.86
0.80 -> 0.86
0.81 -> 0.90
0.82 -> 0.90
0.83 -> 0.90
0.84 -> 0.90
0.85 -> 0.90
0.86 -> 0.94
0.87 -> 0.94
0.88 -> 0.94
0.89 -> 0.94
0.90 -> 0.94
0.91 -> 0.98
0.92 -> 0.98
0.93 -> 0.98
0.94 -> 0.98
0.95 -> 0.98
0.96 -> 1.00
0.97 -> 1.00
0.98 -> 1.00
0.99 -> 1.00
 
</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Price_fraction#Pascal Pascal].
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
n[] = [ 10 18 26 32 38 44 50 54 58 62 66 70 74 78 82 86 90 94 98 100 ]
func conv p .
cat = (p - 1) div 5 + 1
return n[cat]
.
for in = 5 step 5 to 100
if in = 100
in$ = "1.00"
elif in < 10
in$ = "0.0" & in
else
in$ = "0." & in
.
out = conv in
if out = 100
out$ = "1.00"
else
out$ = "0." & out
.
print in$ & " -> " & out$
.
</syntaxhighlight>
{{out}}
<pre>
0.05 -> 0.10
0.10 -> 0.18
0.15 -> 0.26
0.20 -> 0.32
0.25 -> 0.38
0.30 -> 0.44
0.35 -> 0.50
0.40 -> 0.54
0.45 -> 0.58
0.50 -> 0.62
0.55 -> 0.66
0.60 -> 0.70
0.65 -> 0.74
0.70 -> 0.78
0.75 -> 0.82
0.80 -> 0.86
0.85 -> 0.90
0.90 -> 0.94
0.95 -> 0.98
1.00 -> 1.00
</pre>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
 
create
make
 
feature
 
make
--Tests the price_adjusted feature.
local
i: REAL
do
create price_fraction.initialize
from
i := 5
until
i = 100
loop
io.put_string ("Given: ")
io.put_real (i / 100)
io.put_string ("%TAdjusted:")
io.put_real (price_fraction.adjusted_price (i / 100))
io.new_line
i := i + 5
end
end
 
price_fraction: PRICE_FRACTION
 
end
</syntaxhighlight>
<syntaxhighlight lang="eiffel">
class
PRICE_FRACTION
 
create
initialize
 
feature
 
initialize
-- Initializes limit and price to the given values.
do
limit := <<0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01>>
price := <<0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62, 0.66, 0.70, 0.74, 0.78, 0.81, 0.86, 0.90, 0.94, 0.98, 1.00>>
end
 
adjusted_price (n: REAL): REAL
-- Adjusted price according to the given price values.
local
i: INTEGER
found: BOOLEAN
do
from
i := 1
until
i > limit.count or found
loop
if n <= limit [i] then
Result := (price [i])
found := True
end
i := i + 1
end
end
 
feature {NONE}
 
limit: ARRAY [REAL]
 
price: ARRAY [REAL]
 
end
</syntaxhighlight>
{{out}}
<pre>
Given: 0.05 Adjusted:0.1
Given: 0.1 Adjusted:0.18
Given: 0.15 Adjusted:0.26
Given: 0.2 Adjusted:0.32
Given: 0.25 Adjusted:0.38
Given: 0.3 Adjusted:0.44
Given: 0.35 Adjusted:0.5
Given: 0.4 Adjusted:0.54
Given: 0.45 Adjusted:0.58
Given: 0.5 Adjusted:0.62
Given: 0.55 Adjusted:0.66
Given: 0.6 Adjusted:0.7
Given: 0.65 Adjusted:0.74
Given: 0.7 Adjusted:0.78
Given: 0.75 Adjusted:0.81
Given: 0.8 Adjusted:0.86
Given: 0.85 Adjusted:0.9
Given: 0.9 Adjusted:0.94
Given: 0.95 Adjusted:0.98
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Price do
@table [ {0.06, 0.10}, {0.11, 0.18}, {0.16, 0.26}, {0.21, 0.32}, {0.26, 0.38},
{0.31, 0.44}, {0.36, 0.50}, {0.41, 0.54}, {0.46, 0.58}, {0.51, 0.62},
{0.56, 0.66}, {0.61, 0.70}, {0.66, 0.74}, {0.71, 0.78}, {0.76, 0.82},
{0.81, 0.86}, {0.86, 0.90}, {0.91, 0.94}, {0.96, 0.98}, {1.01, 1.00} ]
def fraction(value) when value in 0..1 do
{_, standard_value} = Enum.find(@table, fn {upper_limit, _} -> value < upper_limit end)
standard_value
end
end
 
val = for i <- 0..100, do: i/100
Enum.each(val, fn x ->
:io.format "~5.2f ->~5.2f~n", [x, Price.fraction(x)]
end)</syntaxhighlight>
 
{{out}}
<pre>
0.00 -> 0.10
0.01 -> 0.10
0.02 -> 0.10
0.03 -> 0.10
0.04 -> 0.10
0.05 -> 0.10
0.06 -> 0.18
0.07 -> 0.18
0.08 -> 0.18
0.09 -> 0.18
0.10 -> 0.18
0.11 -> 0.26
...
0.95 -> 0.98
0.96 -> 1.00
0.97 -> 1.00
0.98 -> 1.00
0.99 -> 1.00
1.00 -> 1.00
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">priceFraction(N) when N < 0 orelse N > 1 ->
erlang:error('Values must be between 0 and 1.');
priceFraction(N) when N < 0.06 -> 0.10;
Line 761 ⟶ 2,340:
priceFraction(N) when N < 0.91 -> 0.94;
priceFraction(N) when N < 0.96 -> 0.98;
priceFraction(N) -> 1.00.</langsyntaxhighlight>
 
=={{header|Euphoria}}==
{{trans|C}}
<langsyntaxhighlight lang="euphoria">constant table = {
{0.06, 0.10}, {0.11, 0.18}, {0.16, 0.26}, {0.21, 0.32},
{0.26, 0.38}, {0.31, 0.44}, {0.36, 0.50}, {0.41, 0.54},
Line 784 ⟶ 2,363:
for i = 0 to 99 do
printf(1, "%.2f %.2f\n", { i/100, price_fix(i/100) })
end for</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Inspired by Python's bisect solution. Using decimal (System.Decimal) to avoid number representation problems with floats.
<syntaxhighlight lang="fsharp">let cin = [ 0.06m .. 0.05m ..1.01m ]
let cout = [0.1m; 0.18m] @ [0.26m .. 0.06m .. 0.44m] @ [0.50m .. 0.04m .. 0.98m] @ [1.m]
 
let priceadjuster p =
let rec bisect lo hi =
if lo < hi then
let mid = (lo+hi)/2.
let left = p < cin.[int mid]
bisect (if left then lo else mid+1.) (if left then mid else hi)
else lo
 
if p < 0.m || 1.m < p then p
else cout.[int (bisect 0. (float cin.Length))]
 
[ 0.m .. 0.01m .. 1.m ]
|> Seq.ofList
|> Seq.iter (fun p -> printfn "%.2f -> %.2f" p (priceadjuster p))</syntaxhighlight>
{{out}}
The same as shown by Ada as of 2013-11-03T17:42Z (apart from whitespace formatting)
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">CONSTANT: dispensary-data {
{ 0.06 0.10 }
{ 0.11 0.18 }
{ 0.16 0.26 }
{ 0.21 0.32 }
{ 0.26 0.38 }
{ 0.31 0.44 }
{ 0.36 0.50 }
{ 0.41 0.54 }
{ 0.46 0.58 }
{ 0.51 0.62 }
{ 0.56 0.66 }
{ 0.61 0.70 }
{ 0.66 0.74 }
{ 0.71 0.78 }
{ 0.76 0.82 }
{ 0.81 0.86 }
{ 0.86 0.90 }
{ 0.91 0.94 }
{ 0.96 0.98 }
{ 1.01 1.00 } }
 
: price-fraction ( n -- n ) dispensary-data [ first over >= ] find 2nip second ;
 
{ 0 0.5 0.65 0.66 1 } [ price-fraction ] map</syntaxhighlight>
 
{{out}}
 
{ 0.1 0.62 0.74 0.74 1.0 }
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Defn // to hold the three numbers from a 'row' in the table
{
Line 858 ⟶ 2,490:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Line 864 ⟶ 2,496:
A floating-point version wouldn't be hard -- four words would change ( , @ @ cell+ -to- f, f@ f@ float+ ), EVALUATE would be replaced with a small word that forced a floating-point interpretation, and the return stack would not be used in ROUND -- but it would be strikingly unusual. See this page's discussion.
 
<langsyntaxhighlight lang="forth">: as begin parse-word dup while evaluate , repeat 2drop ;
 
create bounds as 96 91 86 81 76 71 66 61 56 51 46 41 36 31 26 21 16 11 6 0
Line 875 ⟶ 2,507:
: round ( n-cents -- n-cents' )
>r bounds begin dup @ r@ > while cell+ repeat
r> drop official@ ;</langsyntaxhighlight>
This one is done in the spirit of "Thinking Forth" and doesn't use any tables at all.
<syntaxhighlight lang="text">: ?adjust 1+ over and if 2* 15 and >r 2 - r> then ;
: accumulate >r dup >r + r> r> ;
: classify dup 0> if 1- then 5 / ;
: calculate do i ?adjust accumulate loop drop drop 100 min ;
: normalize classify >r 0 10 2 r> 1+ 0 calculate ;
: print s>d <# # # [char] . hold #s #> type ;
 
: test cr 101 0 ?do i print i 2 spaces normalize print cr 5 +loop ;
 
test</syntaxhighlight>
Output:
<pre>
0.00 0.10
0.05 0.10
0.10 0.18
0.15 0.26
0.20 0.32
0.25 0.38
0.30 0.44
0.35 0.50
0.40 0.54
0.45 0.58
0.50 0.62
0.55 0.66
0.60 0.70
0.65 0.74
0.70 0.78
0.75 0.82
0.80 0.86
0.85 0.90
0.90 0.94
0.95 0.98
1.00 1.00
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program price_fraction
 
implicit none
Line 897 ⟶ 2,564:
end do
 
end program price_fraction</langsyntaxhighlight>
{{out}}
Sample output:
<syntaxhighlight lang="text">0.997560 1.00
0.566825 0.70
0.965915 1.00
Line 908 ⟶ 2,575:
0.005355 0.10
0.347081 0.50
0.342244 0.50</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn PriceFraction( price as double ) as double
double result
if price < 0.00 or price > 1.00 then exit fn = price
if price < 0.06 then exit fn = 0.10
if price < 0.11 then exit fn = 0.18
if price < 0.16 then exit fn = 0.26
if price < 0.21 then exit fn = 0.32
if price < 0.26 then exit fn = 0.38
if price < 0.31 then exit fn = 0.44
if price < 0.36 then exit fn = 0.50
if price < 0.41 then exit fn = 0.54
if price < 0.46 then exit fn = 0.58
if price < 0.51 then exit fn = 0.62
if price < 0.56 then exit fn = 0.66
if price < 0.61 then exit fn = 0.70
if price < 0.66 then exit fn = 0.74
if price < 0.71 then exit fn = 0.78
if price < 0.76 then exit fn = 0.82
if price < 0.81 then exit fn = 0.86
if price < 0.86 then exit fn = 0.90
if price < 0.91 then exit fn = 0.94
if price < 0.96 then exit fn = 0.98
result = 1.00
end fn = result
 
void local fn GetPriceFractions
NSUInteger i
for i = 1 to 100
double d = i/100.0
printf @"%.2f -> %.2f\t\b", d, fn PriceFraction( d )
if i mod 5 == 0 then print
next
end fn
 
fn GetPriceFractions
 
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
0.01 -> 0.10 0.02 -> 0.10 0.03 -> 0.10 0.04 -> 0.10 0.05 -> 0.10
0.06 -> 0.18 0.07 -> 0.18 0.08 -> 0.18 0.09 -> 0.18 0.10 -> 0.18
0.11 -> 0.26 0.12 -> 0.26 0.13 -> 0.26 0.14 -> 0.26 0.15 -> 0.26
0.16 -> 0.32 0.17 -> 0.32 0.18 -> 0.32 0.19 -> 0.32 0.20 -> 0.32
0.21 -> 0.38 0.22 -> 0.38 0.23 -> 0.38 0.24 -> 0.38 0.25 -> 0.38
0.26 -> 0.44 0.27 -> 0.44 0.28 -> 0.44 0.29 -> 0.44 0.30 -> 0.44
0.31 -> 0.50 0.32 -> 0.50 0.33 -> 0.50 0.34 -> 0.50 0.35 -> 0.50
0.36 -> 0.54 0.37 -> 0.54 0.38 -> 0.54 0.39 -> 0.54 0.40 -> 0.54
0.41 -> 0.58 0.42 -> 0.58 0.43 -> 0.58 0.44 -> 0.58 0.45 -> 0.58
0.46 -> 0.62 0.47 -> 0.62 0.48 -> 0.62 0.49 -> 0.62 0.50 -> 0.62
0.51 -> 0.66 0.52 -> 0.66 0.53 -> 0.66 0.54 -> 0.66 0.55 -> 0.66
0.56 -> 0.70 0.57 -> 0.70 0.58 -> 0.70 0.59 -> 0.70 0.60 -> 0.70
0.61 -> 0.74 0.62 -> 0.74 0.63 -> 0.74 0.64 -> 0.74 0.65 -> 0.74
0.66 -> 0.78 0.67 -> 0.78 0.68 -> 0.78 0.69 -> 0.78 0.70 -> 0.78
0.71 -> 0.82 0.72 -> 0.82 0.73 -> 0.82 0.74 -> 0.82 0.75 -> 0.82
0.76 -> 0.86 0.77 -> 0.86 0.78 -> 0.86 0.79 -> 0.86 0.80 -> 0.86
0.81 -> 0.90 0.82 -> 0.90 0.83 -> 0.90 0.84 -> 0.90 0.85 -> 0.90
0.86 -> 0.94 0.87 -> 0.94 0.88 -> 0.94 0.89 -> 0.94 0.90 -> 0.94
0.91 -> 0.98 0.92 -> 0.98 0.93 -> 0.98 0.94 -> 0.98 0.95 -> 0.98
0.96 -> 1.00 0.97 -> 1.00 0.98 -> 1.00 0.99 -> 1.00 1.00 -> 1.00
</pre>
 
 
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
<lang go>func pf(v float64) float64 {
 
import "fmt"
 
func pf(v float64) float64 {
switch {
case v < .06: return .10
case v < .11: return .1810
case v < .1611: return .26
case v < .21: return .3218
case v < .2616: return .38
case v < .31: return .4426
case v < .3621: return .50
case v < .41: return .5432
case v < .4626: return .58
case v < .51: return .6238
case v < .5631: return .66
case v < .61: return .7044
case v < .6636: return .74
case v < .71: return .7850
case v < .7641: return .82
case v < .81: return .8654
case v < .8646: return .90
case v < .91: return .9458
case v < .9651: return .98
return .62
case v < .56:
return .66
case v < .61:
return .70
case v < .66:
return .74
case v < .71:
return .78
case v < .76:
return .82
case v < .81:
return .86
case v < .86:
return .90
case v < .91:
return .94
case v < .96:
return .98
}
return 1
}
}</lang>
 
func main() {
tests := []float64{0.3793, 0.4425, 0.0746, 0.6918, 0.2993,
0.5486, 0.7848, 0.9383, 0.2292, 0.9760}
for _, v := range tests {
fmt.Printf("%0.4f -> %0.2f\n", v, pf(v))
}
}</syntaxhighlight>
 
<pre>
0.3793 -> 0.54
0.4425 -> 0.58
0.0746 -> 0.18
0.6918 -> 0.78
0.2993 -> 0.44
0.5486 -> 0.66
0.7848 -> 0.86
0.9383 -> 0.98
0.2292 -> 0.38
0.9760 -> 1.00
</pre>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def priceFraction(value) {
assert value >= 0.0 && value <= 1.0
 
Line 952 ⟶ 2,734:
for (def v = 0.00; v <= 1.00; v += 0.01) {
println "$v --> ${priceFraction(v)}"
}</langsyntaxhighlight>
{{out}}
Output:
<div style="height: 200px;overflow:scroll">
<pre>0.00 --> 0.10
Line 1,059 ⟶ 2,841:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">price_fraction n
| n < 0 || n > 1 = error "Values must be between 0 and 1."
| n < 0.06 = 0.10
Line 1,080 ⟶ 2,862:
| n < 0.91 = 0.94
| n < 0.96 = 0.98
| otherwise = 1.00</langsyntaxhighlight>
Alternative {{trans|OCaml}}:
<langsyntaxhighlight lang="haskell">table = [
(0.06, 0.10), (0.11, 0.18), (0.16, 0.26), (0.21, 0.32), (0.26, 0.38),
(0.31, 0.44), (0.36, 0.50), (0.41, 0.54), (0.46, 0.58), (0.51, 0.62),
Line 1,091 ⟶ 2,873:
price_fraction n
| n < 0 || n > 1 = error "Values must be between 0 and 1."
| otherwise = snd $ head $ dropWhile ((<= n) . fst) table</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">DIMENSION upperbound(20), rescaleTo(20), temp(20)
upperbound = (.06,.11,.16,.21,.26,.31,.36,.41,.46,.51,.56,.61,.66,.71,.76,.81,.86,.91,.96,1.01)
rescaleTo = (.10,.18,.26,.32,.38,.44,.50,.54,.58,.62,.66,.70,.74,.78,.82,.86,.90,.94,.98,1.00)
Line 1,103 ⟶ 2,885:
PriceFraction = rescaleTo( INDEX(temp, 0) )
WRITE(Format="F8.6, F6.2") value, PriceFraction
ENDDO</langsyntaxhighlight>
<pre>0.589230 0.70
0.017623 0.10
Line 1,117 ⟶ 2,899:
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="icon">
<lang Icon>
record Bounds(low,high,new)
 
Line 1,157 ⟶ 2,939:
}
end
</syntaxhighlight>
</lang>
 
{{out}}
Output:
<pre>
0.0 rescaled is 0.1
Line 1,177 ⟶ 2,959:
Inform doesn't have native floating-point support; this version uses fixed point numbers with two decimal places.
 
<langsyntaxhighlight lang="inform7">Home is a room.
 
Price is a kind of value. 0.99 specifies a price.
Line 1,213 ⟶ 2,995:
let P be a random price between 0.00 and 1.00;
say "[P] -> [standardized value of P].";
end the story.</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">le =: -0.96 0.91 0.86 0.81 0.76 0.71 0.66 0.61 0.56 0.51 0.46 0.41 0.36 0.31 0.26 0.21 0.16 0.11 0.06 0.0
out =: 1.00 0.98 0.94 0.90 0.86 0.82 0.78 0.74 0.70 0.66 0.62 0.58 0.54 0.50 0.44 0.38 0.32 0.26 0.18 0.1
 
priceFraction =: out {~ le I. -</langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight lang="j"> priceFraction 0.34 0.070145 0.06 0.05 0.50214 0.56 1 0.99 0
0.5 0.18 0.18 0.1 0.62 0.7 1 1 0.1</langsyntaxhighlight>
 
This implementation performs a binary search on the boundary values, and then uses the resulting index to select from the result values.
Line 1,231 ⟶ 3,013:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Random;
 
public class Main {
Line 1,265 ⟶ 3,047:
}
}
}</langsyntaxhighlight>
{{out}}
Output:
<pre>0.149969 -> 0.26
0.310605 -> 0.50
Line 1,272 ⟶ 3,054:
0.194047 -> 0.32
0.724852 -> 0.82</pre>
 
 
=={{header|JavaScript}}==
 
In the task definition, the first step is 0.06, the rest are 0.05 so a re-factoring can subtract 0.01 from the value and divide by 0.05 to get the step.
so a re-factoring can subtract 0.01 from the value and divide by 0.05 to get the step.
 
Working with decimal numbers in JavaScript has issues, e.g. 0.06 - 0.01 = 0.049999999999999996 due to using IEEE 754 double precision numbers that can't accurately represent all decimals. So values are multiplied by 100 and integer arithmetic is used.
Line 1,284 ⟶ 3,066:
Passing a value outside the range 0 <= x < 1.01 will return undefined.
 
<langsyntaxhighlight lang="javascript">function getScaleFactor(v) {
 
var values = ['0.10','0.18','0.26','0.32','0.38','0.44','0.50','0.54',
Line 1,291 ⟶ 3,073:
 
return values[(v * 100 - 1) / 5 | 0];
}</langsyntaxhighlight>
 
=={{header|jq}}==
The solution given here is based on the JavaScript solution.
<syntaxhighlight lang="jq">def getScaleFactor:
["0.10","0.18","0.26","0.32","0.38","0.44","0.50","0.54",
"0.58","0.62","0.66","0.70","0.74","0.78","0.82","0.86",
"0.90","0.94","0.98","1.00"] as $values
| $values[ (. * 100 - 1) / 5 | floor ] ;</syntaxhighlight>
The full coverage test as given in the Ada example:
<syntaxhighlight lang="jq">def test:
(range(0;10) | "0.0\(.) -> \( 0.01 * . | getScaleFactor)"),
(range(10;100) | "0.\(.) -> \( 0.01 * . | getScaleFactor)");
 
test</syntaxhighlight>
Run the test, showing the first few lines of output:
<pre>
$ jq -n -r -f Price_fraction.jq
0.00 -> 1.00
0.01 -> 0.10
0.02 -> 0.10
0.03 -> 0.10
0.04 -> 0.10
0.05 -> 0.10
0.06 -> 0.18
0.07 -> 0.18
0.08 -> 0.18
0.09 -> 0.18
0.10 -> 0.18
0.11 -> 0.26
...</pre>
 
=={{header|Julia}}==
This solution is somewhat straightforward but does highlight a couple of Julia features. The interval cut-offs and values are exactly represented by rational numbers. The interval to which an input value belongs is identified by applying the <code>findfirst</code> (true value) function to an element-wise comparison (<code>.&lt;</code>) of this value to the cut-off array.
<syntaxhighlight lang="julia">
const PFCUT = [6:5:101]//100
const PFVAL = [10:8:26, 32:6:50, 54:4:98, 100]//100
 
function pricefraction{T<:FloatingPoint}(a::T)
zero(T) <= a || error("a = ", a, ", but it must be >= 0.")
a <= one(T) || error("a = ", a, ", but it must be <= 1.")
convert(T, PFVAL[findfirst(a .< PFCUT)])
end
 
test = [0.:0.05:1., 0.51, 0.56, 0.61, rand(), rand(), rand(), rand()]
 
println("Testing the price fraction function")
for t in test
println(@sprintf " %.4f -> %.4f" t pricefraction(t))
end
</syntaxhighlight>
 
{{out}}
<pre>
Testing the price fraction function
0.0000 -> 0.1000
0.0500 -> 0.1000
0.1000 -> 0.1800
0.1500 -> 0.2600
0.2000 -> 0.3200
0.2500 -> 0.3800
0.3000 -> 0.4400
0.3500 -> 0.5000
0.4000 -> 0.5400
0.4500 -> 0.5800
0.5000 -> 0.6200
0.5500 -> 0.6600
0.6000 -> 0.7000
0.6500 -> 0.7400
0.7000 -> 0.7800
0.7500 -> 0.8200
0.8000 -> 0.8600
0.8500 -> 0.9000
0.9000 -> 0.9400
0.9500 -> 0.9800
1.0000 -> 1.0000
0.5100 -> 0.6600
0.5600 -> 0.7000
0.6100 -> 0.7400
0.5603 -> 0.7000
0.9812 -> 1.0000
0.5127 -> 0.6600
0.4821 -> 0.6200
</pre>
 
=={{header|K}}==
Translation of the J solution:
 
<syntaxhighlight lang="k">
<lang K>
le:- 0.96 0.91 0.86 0.81 0.76 0.71 0.66 0.61 0.56 0.51 0.46 0.41 0.36 0.31 0.26 0.21 0.16 0.11 0.06 0.0
out: 1.00 0.98 0.94 0.90 0.86 0.82 0.78 0.74 0.70 0.66 0.62 0.58 0.54 0.50 0.44 0.38 0.32 0.26 0.18 0.1
 
pf:{out@_bin[le;-x]}'
</syntaxhighlight>
</lang>
{{out}}
Output
<lang Kpre>
pf 0.6094701 0.5003597 0.8512954 0.08951883 0.6868076
0.7 0.62 0.9 0.18 0.78
</langpre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
 
fun rescale(price: Double): Double =
when {
price < 0.06 -> 0.10
price < 0.11 -> 0.18
price < 0.16 -> 0.26
price < 0.21 -> 0.32
price < 0.26 -> 0.38
price < 0.31 -> 0.44
price < 0.36 -> 0.50
price < 0.41 -> 0.54
price < 0.46 -> 0.58
price < 0.51 -> 0.62
price < 0.56 -> 0.66
price < 0.61 -> 0.70
price < 0.66 -> 0.74
price < 0.71 -> 0.78
price < 0.76 -> 0.82
price < 0.81 -> 0.86
price < 0.86 -> 0.90
price < 0.91 -> 0.94
price < 0.96 -> 0.98
else -> 1.00
}
 
fun main(args: Array<String>) {
var d: Double
for (i in 1..100) {
d = i / 100.0
print(String.format("%4.2f -> %4.2f ", d, rescale(d)))
if (i % 5 == 0) println()
}
}</syntaxhighlight>
 
{{out}}
<pre>
0.01 -> 0.10 0.02 -> 0.10 0.03 -> 0.10 0.04 -> 0.10 0.05 -> 0.10
0.06 -> 0.18 0.07 -> 0.18 0.08 -> 0.18 0.09 -> 0.18 0.10 -> 0.18
0.11 -> 0.26 0.12 -> 0.26 0.13 -> 0.26 0.14 -> 0.26 0.15 -> 0.26
0.16 -> 0.32 0.17 -> 0.32 0.18 -> 0.32 0.19 -> 0.32 0.20 -> 0.32
0.21 -> 0.38 0.22 -> 0.38 0.23 -> 0.38 0.24 -> 0.38 0.25 -> 0.38
0.26 -> 0.44 0.27 -> 0.44 0.28 -> 0.44 0.29 -> 0.44 0.30 -> 0.44
0.31 -> 0.50 0.32 -> 0.50 0.33 -> 0.50 0.34 -> 0.50 0.35 -> 0.50
0.36 -> 0.54 0.37 -> 0.54 0.38 -> 0.54 0.39 -> 0.54 0.40 -> 0.54
0.41 -> 0.58 0.42 -> 0.58 0.43 -> 0.58 0.44 -> 0.58 0.45 -> 0.58
0.46 -> 0.62 0.47 -> 0.62 0.48 -> 0.62 0.49 -> 0.62 0.50 -> 0.62
0.51 -> 0.66 0.52 -> 0.66 0.53 -> 0.66 0.54 -> 0.66 0.55 -> 0.66
0.56 -> 0.70 0.57 -> 0.70 0.58 -> 0.70 0.59 -> 0.70 0.60 -> 0.70
0.61 -> 0.74 0.62 -> 0.74 0.63 -> 0.74 0.64 -> 0.74 0.65 -> 0.74
0.66 -> 0.78 0.67 -> 0.78 0.68 -> 0.78 0.69 -> 0.78 0.70 -> 0.78
0.71 -> 0.82 0.72 -> 0.82 0.73 -> 0.82 0.74 -> 0.82 0.75 -> 0.82
0.76 -> 0.86 0.77 -> 0.86 0.78 -> 0.86 0.79 -> 0.86 0.80 -> 0.86
0.81 -> 0.90 0.82 -> 0.90 0.83 -> 0.90 0.84 -> 0.90 0.85 -> 0.90
0.86 -> 0.94 0.87 -> 0.94 0.88 -> 0.94 0.89 -> 0.94 0.90 -> 0.94
0.91 -> 0.98 0.92 -> 0.98 0.93 -> 0.98 0.94 -> 0.98 0.95 -> 0.98
0.96 -> 1.00 0.97 -> 1.00 0.98 -> 1.00 0.99 -> 1.00 1.00 -> 1.00
</pre>
 
=={{header|langur}}==
Langur uses decimal floating point.
 
<syntaxhighlight lang="langur">val .pricefrac = fn(.f) { switch[and] .f {
case >= 0.00, < 0.06: 0.10
case >= 0.06, < 0.11: 0.18
case >= 0.11, < 0.16: 0.26
case >= 0.16, < 0.21: 0.32
case >= 0.21, < 0.26: 0.38
case >= 0.26, < 0.31: 0.44
case >= 0.31, < 0.36: 0.50
case >= 0.36, < 0.41: 0.54
case >= 0.41, < 0.46: 0.58
case >= 0.46, < 0.51: 0.62
case >= 0.51, < 0.56: 0.66
case >= 0.56, < 0.61: 0.70
case >= 0.61, < 0.66: 0.74
case >= 0.66, < 0.71: 0.78
case >= 0.71, < 0.76: 0.82
case >= 0.76, < 0.81: 0.86
case >= 0.81, < 0.86: 0.90
case >= 0.86, < 0.91: 0.94
case >= 0.91, < 0.96: 0.98
case >= 0.96, <= 1.00: 1.00
default: throw "bad data"
}}
 
writeln .pricefrac(0.17)
writeln .pricefrac(0.71)</syntaxhighlight>
 
{{out}}
<pre>0.32
0.82</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">scaleTable = {
{0.06, 0.10}, {0.11, 0.18}, {0.16, 0.26}, {0.21, 0.32},
{0.26, 0.38}, {0.31, 0.44}, {0.36, 0.50}, {0.41, 0.54},
{0.46, 0.58}, {0.51, 0.62}, {0.56, 0.66}, {0.61, 0.70},
{0.66, 0.74}, {0.71, 0.78}, {0.76, 0.82}, {0.81, 0.86},
{0.86, 0.90}, {0.91, 0.94}, {0.96, 0.98}, {1.01, 1.00}
}
function rescale (price)
if price < 0 or price > 1 then return "Out of range!" end
for k, v in pairs(scaleTable) do
if price < v[1] then return v[2] end
end
end
 
math.randomseed(os.time())
for i = 1, 5 do
rnd = math.random()
print("Random value:", rnd)
print("Adjusted price:", rescale(rnd))
print()
end</syntaxhighlight>
{{out}}
<pre>Random value: 0.61946413522022
Adjusted price: 0.74
 
Random value: 0.81141947958698
Adjusted price: 0.9
 
Random value: 0.55691473099814
Adjusted price: 0.66
 
Random value: 0.19704311677601
Adjusted price: 0.32
 
Random value: 0.36528313938816
Adjusted price: 0.54</pre>
 
=={{header|M2000 Interpreter}}==
Derived from BASIC
 
<syntaxhighlight lang="m2000 interpreter">
Module PriceFraction {
Currency i
Print $("0.00"),
for i=0@ to 1@ step .10@
Print i, @PriceFraction(i)
next
Print $(""),
function PriceFraction(price as currency)
select case price
case < 0
= price
case < .06
= .1
case < .11
= .18
case < .16
= .26
case < .21
= .32
case < .26
= .38
case < .31
= .44
case < .36
= .5
case < .41
= .54
case < .46
= .58
case < .51
= .62
case < .56
= .66
case < .61
= .7
case < .66
= .74
case < .71
= .78
case < .76
= .82
case < .81
= .86
case < .86
= .9
case < .91
= .94
case < .96
= .98
case < 1.01
= 1!
case else
= price
end select
end function
}
PriceFraction
</syntaxhighlight>
{{out}}
<pre>
0.00 0.10
0.10 0.18
0.20 0.32
0.30 0.44
0.40 0.54
0.50 0.62
0.60 0.70
0.70 0.78
0.80 0.86
0.90 0.94
1.00 1.00
</pre>
 
 
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">priceFraction := proc(price)
local values, standard, newPrice, i;
values := [0, 0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61,
0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01];
standard := [0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62, 0.66, 0.70,
0.74, 0.78, 0.82, 0.86, 0.90, 0.94, 0.98, 1.00];
for i to numelems(standard) do
if price >= values[i] and price < values[i+1] then
newPrice := standard[i];
end if;
end do;
printf("%f --> %.2f\n", price, newPrice);
end proc:
 
randomize():
for i to 5 do
priceFraction (rand(0.0..1.0)());
end do;</syntaxhighlight>
{{out}}
<pre>0.524386 --> 0.66
0.887957 --> 0.94
0.670196 --> 0.78
0.875601 --> 0.94
0.540447 --> 0.66</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">PriceFraction[x_]:=Piecewise[{{.1, 0 <= x < 0.06}, {.18, x < .11}, {.26,x < 0.16},
{.32, x < .21}, {.38, x < .26}, {.44, x < 0.31}, {.5, x < .36},
{.54, x < .41}, {.58, x < .46}, {.62, x < .51}, {.66, x < .56},
{.70, x < .61}, {.74, x < .66}, {.78, x < .71}, {.82, x < .76},
{.86, x < .81}, {.90, x < .86}, {.94, x < .91}, {.98, x < .96}}, 1]</langsyntaxhighlight>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
 
<langsyntaxhighlight Matlablang="matlab"> function y = rescale(x)
L = [0,.06:.05:1.02];
Line 1,330 ⟶ 3,433:
 
t=0:0.001:1;
plot(t,rescale(t)); </langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight Mercurylang="mercury">:- module price.
:- interface.
:- import_module int.
Line 1,376 ⟶ 3,479:
rule(86, 91, 94),
rule(91, 96, 98),
rule(96, 101, 100)].</langsyntaxhighlight>
 
A build system might turn the text of the table into the definition of a hundred-element array of adjustments. In that case,
 
<langsyntaxhighlight Mercurylang="mercury">adjust(Cents) = array.lookup(price_table, Cents).</langsyntaxhighlight>
 
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">PRICFRAC(X)
;Outputs a specified value dependent upon the input value
;The non-inclusive upper limits are encoded in the PFMAX string, and the values
Line 1,393 ⟶ 3,497:
FOR I=1:1:$LENGTH(PFMAX,"^") Q:($DATA(RESULT)'=0) SET:X<$P(PFMAX,"^",I) RESULT=$P(PFRES,"^",I)
KILL PFMAX,PFRES,I
QUIT RESULT</langsyntaxhighlight>
{{out}}
Output:<pre>USER>W $$PRICFRAC^ROSETTA(.04)
<pre>USER>W $$PRICFRAC^ROSETTA(.04)
.10
USER>W $$PRICFRAC^ROSETTA(.06)
Line 1,404 ⟶ 3,509:
USER>W $$PRICFRAC^ROSETTA(.81)
.90</pre>
 
=={{header|NetRexx}}==
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
runSample(arg)
return
 
-- -----------------------------------------------------------------------------
method runSample(arg) public static
parse arg in_val .
if in_val \= '' then test_vals = [in_val]
else test_vals = getTestData()
 
say 'Input Adjustment'
loop tv = 0 to test_vals.length - 1
in_val = test_vals[tv]
adjust = priceFraction(in_val)
say in_val.format(null, 2).right(5) adjust.format(null, 2).right(10)
end tv
 
return
 
-- -----------------------------------------------------------------------------
method priceFraction(in_val) public static
out_val = -1
limit_table = getLimitTable()
limit_table_K = limit_table.length
loop p1 = 0 to limit_table_K - 1
pair = limit_table[p1]
hi_limit = pair[0]
adjustmt = pair[1]
if in_val < hi_limit then do
out_val = adjustmt
leave p1
end
end p1
if out_val = -1 then signal IllegalArgumentException('Input' in_val 'is outside of acceptable range.')
 
return out_val
 
-- -----------------------------------------------------------------------------
method getLimitTable() public static returns Rexx[,]
limit_table = [ -
[0.06, 0.10], [0.11, 0.18], [0.16, 0.26], [0.21, 0.32], [0.26, 0.38], -
[0.31, 0.44], [0.36, 0.50], [0.41, 0.54], [0.46, 0.58], [0.51, 0.62], -
[0.56, 0.66], [0.61, 0.70], [0.66, 0.74], [0.71, 0.78], [0.76, 0.82], -
[0.81, 0.86], [0.86, 0.90], [0.91, 0.94], [0.96, 0.98], [1.01, 1.00] -
]
return limit_table
 
-- -----------------------------------------------------------------------------
method getTestData() private static returns Rexx[]
test_vals = Rexx[5]
rng = Random(1024)
loop tv = 0 to test_vals.length - 1
test_vals[tv] = rng.nextFloat()
end tv
return test_vals
</syntaxhighlight>
{{out}}
<pre>
Input Adjustment
0.64 0.74
0.32 0.50
0.85 0.90
0.93 0.98
0.62 0.74
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import random, strformat
 
# Representation of a standard value as an int (actual value * 100).
type StandardValue = distinct int
 
proc `<`(a, b: StandardValue): bool {.borrow.}
 
const Pricemap = [10, 18, 26, 32, 38, 44, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 100]
 
 
proc toStandardValue(f: float): StandardValue =
## Convert a float to a standard value (decimal value multiplied by 100).
## Index: 0.01..0.05 -> 0, 0.06..0.10 -> 1, 0.11..0.15 -> 2...
var value = int(f * 100)
if value == 0: return StandardValue(10)
dec value
# Increment index every 5 of value, so value in 1..100 translates to index in 0..19.
let index = 2 * (value div 10) + (value mod 10) div 5
result = StandardValue(Pricemap[index])
 
 
proc `$`(price: StandardValue): string =
## Return the string representation of a standard value.
if price < StandardValue(10): "0.0" & $int(price)
elif price < StandardValue(100): "0." & $int(price)
else: "1.00"
 
 
when isMainModule:
randomize()
for _ in 0 .. 10:
let price = rand(1.01)
echo &"Price for {price:.2f} is {price.toStandardValue()}"</syntaxhighlight>
{{out}}
A random output looking something like this:
<pre>Price for 0.88 is 0.94
Price for 0.58 is 0.70
Price for 0.67 is 0.78
Price for 0.53 is 0.66
Price for 0.56 is 0.66
Price for 0.02 is 0.10
Price for 0.61 is 0.70
Price for 0.41 is 0.58
Price for 0.22 is 0.38
Price for 0.91 is 0.98
Price for 0.42 is 0.58</pre>
 
=={{header|Objeck}}==
{{trans|C#}}
<syntaxhighlight lang="objeck">class PriceFraction {
function : Main(args : String[]) ~ Nil {
for(i := 0; i < 5; i++;) {
f := Float->Random();
r := SpecialRound(f);
"{$f} -> {$r}"->PrintLine();
};
}
 
function : SpecialRound(inValue : Float) ~ Float {
if (inValue > 1) {
return 1;
};
 
splitters := [
0.00 , 0.06 , 0.11 , 0.16 , 0.21 ,
0.26 , 0.31 , 0.36 , 0.41 , 0.46 ,
0.51 , 0.56 , 0.61 , 0.66 , 0.71 ,
0.76 , 0.81 , 0.86 , 0.91 , 0.96 ];
 
replacements := [
0.10 , 0.18 , 0.26 , 0.32 , 0.38 ,
0.44 , 0.50 , 0.54 , 0.58 , 0.62 ,
0.66 , 0.70 , 0.74 , 0.78 , 0.82 ,
0.86 , 0.90 , 0.94 , 0.98 , 1.00 ];
 
for(x := 0; x < splitters->Size() - 1; x+=1;) {
if (inValue >= splitters[x] & inValue < splitters[x + 1]) {
return replacements[x];
};
};
 
return inValue;
}
}</syntaxhighlight>
 
{{output}}
<pre>
0.317901 -> 0.5
0.691109 -> 0.78
0.790891 -> 0.86
0.269922 -> 0.44
0.690891 -> 0.78
</pre>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let price_fraction v =
if v < 0.0 || v >= 1.01 then
invalid_arg "price_fraction";
Line 1,421 ⟶ 3,690:
0.56, 0.66; 0.61, 0.70; 0.66, 0.74; 0.71, 0.78; 0.76, 0.82;
0.81, 0.86; 0.86, 0.90; 0.91, 0.94; 0.96, 0.98; 1.01, 1.00;
]</langsyntaxhighlight>
 
<langsyntaxhighlight lang="ocaml">let () =
let ok_tests = [
(0.3793, 0.54);
Line 1,440 ⟶ 3,709:
Printf.printf " %6g %g %b\n" v r (r = ok);
) ok_tests;
;;</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">[.06, .11, .16, .21, .26, .31, .36, .41, .46, .51, .56, .61, .66, .71, .76, .81, .86, .91, .96, 1.01] const: IN
[.10, .18, .26, .32, .38, .44, .50, .54, .58, .62, .66, .70, .74, .78, .82, .86, .90, .94, .98, 1.00] const: OUT
 
: priceFraction(f)
| i |
IN size loop: i [ f IN at(i) < ifTrue: [ OUT at(i) return ] ]
null ;</syntaxhighlight>
 
{{Out}}
<pre>
>[0.7388727, 0.8593103, 0.826687, 0.3444635] map(#priceFraction) .
[0.82, 0.9, 0.9, 0.5] ok
</pre>
 
=={{header|Oz}}==
Using a for-loop with return and a default value for values >= 1.01. For out-of-range input, a "failed value" is returned, i.e. a value that throws an exception when it is accessed.
For out-of-range input, a "failed value" is returned,
i.e. a value that throws an exception when it is accessed.
 
<langsyntaxhighlight lang="oz">fun {PriceFraction X}
OutOfRange = {Value.failed outOfRange(X)}
in
Line 1,460 ⟶ 3,746:
if X < Limit then {Return Result} end
end
end</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">priceLookup=[6,11,16,21,26,31,41,46,51,56,61,66,71,76,81,86,91,96,101];
priceReplace=[10,18,26,32,38,44,50,54,58,62,66,70,74,78,82,86,90,94,98,100];
pf(x)={
Line 1,471 ⟶ 3,757:
);
"nasal demons"
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program PriceFraction(output);
 
const
Line 1,498 ⟶ 3,784:
writeln (cost:6:4, ' -> ', price[j+1]:4:2);
end;
end.</langsyntaxhighlight>
{{out}}
Output:
<pre>% ./PriceFraction
0.8145 -> 0.90
Line 1,513 ⟶ 3,799:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">my @table = map [ /([\d\.]+)/g ], split "\n", <<'TBL';
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
Line 1,549 ⟶ 3,835:
printf "%.3f -> %g\n", $m, convert($m);
}
</syntaxhighlight>
</lang>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang perl6>my $table = "
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
>= 0.00 < 0.06 := 0.10
<span style="color: #008080;">constant</span> <span style="color: #000000;">TBL</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"""
>= 0.06 < 0.11 := 0.18
> &gt;= 0.1100 <&lt; 0.1606 := 0.2610
> &gt;= 0.1606 <&lt; 0.2111 := 0.3218
> &gt;= 0.2111 <&lt; 0.2616 := 0.3826
> &gt;= 0.2616 <&lt; 0.3121 := 0.4432
> &gt;= 0.3121 <&lt; 0.3626 := 0.5038
> &gt;= 0.3626 <&lt; 0.4131 := 0.5444
> &gt;= 0.4131 <&lt; 0.4636 := 0.5850
> &gt;= 0.4636 <&lt; 0.5141 := 0.6254
> &gt;= 0.5141 <&lt; 0.5646 := 0.6658
> &gt;= 0.5646 <&lt; 0.6151 := 0.7062
> &gt;= 0.6151 <&lt; 0.6656 := 0.7466
> &gt;= 0.6656 <&lt; 0.7161 := 0.7870
> &gt;= 0.7161 <&lt; 0.7666 := 0.8274
> &gt;= 0.7666 <&lt; 0.8171 := 0.8678
> &gt;= 0.8171 <&lt; 0.8676 := 0.9082
> &gt;= 0.8676 <&lt; 0.9181 := 0.9486
> &gt;= 0.9181 <&lt; 0.9686 := 0.9890
> &gt;= 0.9686 <&lt; 10.0191 := 10.0094
&gt;= 0.91 &lt; 0.96 := 0.98
";
&gt;= 0.96 &lt; 1.01 := 1.00"""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">limits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">prices</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">pl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plt</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">price</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">TBL</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">pl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">price</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">TBL</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"&gt;= %.2f &lt; %.2f := %.2f"</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pl</span><span style="color: #0000FF;">==</span><span style="color: #000000;">plt</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">plt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lt</span>
<span style="color: #000000;">limits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lt</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">prices</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prices</span><span style="color: #0000FF;">,</span><span style="color: #000000;">price</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">price_fix</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">p</span><span style="color: #0000FF;"><</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">prices</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">101</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%5.2f %5.2f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">price_fix</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
my $value = 0.44;
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt
 
(
say price($value);
( 0.00 0.06 0.10 )
( 0.06 0.11 0.18 )
( 0.11 0.16 0.26 )
( 0.16 0.21 0.32 )
( 0.21 0.26 0.38 )
( 0.26 0.31 0.44 )
( 0.31 0.36 0.50 )
( 0.36 0.41 0.54 )
( 0.41 0.46 0.58 )
( 0.46 0.51 0.62 )
( 0.51 0.56 0.66 )
( 0.56 0.61 0.70 )
( 0.61 0.66 0.74 )
( 0.66 0.71 0.78 )
( 0.71 0.76 0.82 )
( 0.76 0.81 0.86 )
( 0.81 0.86 0.90 )
( 0.86 0.91 0.94 )
( 0.91 0.96 0.98 )
( 0.96 1.01 1.00 ) )
 
def price_fix
sub price($value)
var p
{
len for
for $table.lines -> $line {
get
$line ~~ / '>=' \s+ (\S+) \s+ '<' \s+ (\S+) \s+ ':=' \s+ (\S+)/;
3 get swap 1 get swap 2 get nip
return $2 if $0 <= $value < $1;
p swap < swap p swap >=
}
and if
fail "Out of range";
exitfor
}</lang>
else
Perhaps a better approach is just to build an array of 101 entries. Memory is cheap, and array lookup is blazing fast, especially important if used in a loop as below. Moreover, in Perl&nbsp;6 we don't have to worry about floating point misrepresentations of decimals because decimal fractions are stored as rationals.
drop
endif
endfor
enddef
 
( 0.00 1.01 0.01 ) for
<lang perl6>my @price = map *.value,
dup print 9 tochar print price_fix print nl
( 0 ..^ 6 X=> 0.10),
endfor
( 6 ..^ 11 X=> 0.18),
</syntaxhighlight>
(11 ..^ 16 X=> 0.26),
(16 ..^ 21 X=> 0.32),
(21 ..^ 26 X=> 0.38),
(26 ..^ 31 X=> 0.44),
(31 ..^ 36 X=> 0.50),
(36 ..^ 41 X=> 0.54),
(41 ..^ 46 X=> 0.58),
(46 ..^ 51 X=> 0.62),
(51 ..^ 56 X=> 0.66),
(56 ..^ 61 X=> 0.70),
(61 ..^ 66 X=> 0.74),
(66 ..^ 71 X=> 0.78),
(71 ..^ 76 X=> 0.82),
(76 ..^ 81 X=> 0.86),
(81 ..^ 86 X=> 0.90),
(86 ..^ 91 X=> 0.94),
(91 ..^ 96 X=> 0.98),
(96 ..^101 X=> 1.00),
;
 
=={{header|Picat}}==
while prompt("value: ") -> $value {
===Approach 1===
say @price[ $value * 100 ] // note "Out of range";
<syntaxhighlight lang="picat">go =>
}</lang>
_ = random2(),
D = [
[0.00,0.06,0.10],
[0.06,0.11,0.18],
[0.11,0.16,0.26],
[0.16,0.21,0.32],
[0.21,0.26,0.38],
[0.26,0.31,0.44],
[0.31,0.36,0.50],
[0.36,0.41,0.54],
[0.41,0.46,0.58],
[0.46,0.51,0.62],
[0.51,0.56,0.66],
[0.56,0.61,0.70],
[0.61,0.66,0.74],
[0.66,0.71,0.78],
[0.71,0.76,0.82],
[0.76,0.81,0.86],
[0.81,0.86,0.90],
[0.86,0.91,0.94],
[0.91,0.96,0.98],
[0.96,1.01,1.00]],
 
Len = D.length,
Yet another approach is to use the conditional operator to encode the table.
foreach(_ in 1..10)
This allows each endpoint to be written once, avoiding duplication.
R = frand2(100),
<lang perl6>sub price_fraction ( Num $n where { $^n >= 0 and $^n <= 1 } ) {
between(1,Len,Ix),
( $n < 0.06 ) ?? 0.10
R >= D[Ix,1],
!! ( $n < 0.11 ) ?? 0.18
!! ( $nR < 0.16 ) ?? 0.26D[Ix,2],
New = D[Ix,3],
!! ( $n < 0.21 ) ?? 0.32
println(R=New)
!! ( $n < 0.26 ) ?? 0.38
end,
!! ( $n < 0.31 ) ?? 0.44
nl.
!! ( $n < 0.36 ) ?? 0.50
!! ( $n < 0.41 ) ?? 0.54
!! ( $n < 0.46 ) ?? 0.58
!! ( $n < 0.51 ) ?? 0.62
!! ( $n < 0.56 ) ?? 0.66
!! ( $n < 0.61 ) ?? 0.70
!! ( $n < 0.66 ) ?? 0.74
!! ( $n < 0.71 ) ?? 0.78
!! ( $n < 0.76 ) ?? 0.82
!! ( $n < 0.81 ) ?? 0.86
!! ( $n < 0.86 ) ?? 0.90
!! ( $n < 0.91 ) ?? 0.94
!! ( $n < 0.96 ) ?? 0.98
!! 1.00
;
}
 
% Getting numbers of precision 2
while prompt("value: ") -> $value {
frand2(N) = (random() mod N)/N.</syntaxhighlight>
last if $value ~~ /exit|quit/;
say price_fraction(+$value);
}</lang>
 
=={{header|PL/Iout}}==
<pre>0.2 = 0.32
<lang PL/I>
0.91 = 0.98
declare t(20) fixed decimal (3,2) static initial (
0.44 = 0.58
.06, .11, .16, .21, .26, .31, .36, .41, .46, .51,
0.81 = 0.9
.56, .61, .66, .71, .76, .81, .86, .91, .96, 1.01);
0.74 = 0.82
declare r(20) fixed decimal (3,2) static initial (
0.1 = 0.18
.10, .18, .26, .32, .38, .44, .50, .54, .58, .62,
0.42 = 0.58
.66, .70, .74, .78, .82, .86, .90, .94, .98, 1);
0.93 = 0.98
declare x float, d fixed decimal (3,2);
0.14 = 0.26
declare i fixed binary;
0.62 = 0.74</pre>
 
===Using a fact table===
<syntaxhighlight lang="picat">go2 =>
_ = random2(),
foreach(_ in 1..10)
R = frand2(100),
r(From,To,Val),
R >= From,
R <= To,
println(R=Val)
end,
 
nl.
loop:
 
do i = 1 to 20;
r(0.00,0.06,0.10).
if x < t(i) then
r(0.06,0.11,0.18).
do; d = r(i); leave loop; end;
r(0.11,0.16,0.26).
end;
r(0.16,0.21,0.32).
</lang>
r(0.21,0.26,0.38).
r(0.26,0.31,0.44).
r(0.31,0.36,0.50).
r(0.36,0.41,0.54).
r(0.41,0.46,0.58).
r(0.46,0.51,0.62).
r(0.51,0.56,0.66).
r(0.56,0.61,0.70).
r(0.61,0.66,0.74).
r(0.66,0.71,0.78).
r(0.71,0.76,0.82).
r(0.76,0.81,0.86).
r(0.81,0.86,0.90).
r(0.86,0.91,0.94).
r(0.91,0.96,0.98).
r(0.96,1.01,1.00).</syntaxhighlight>
 
{{out}}
<pre>0.83 = 0.9
0.77 = 0.86
0.65 = 0.74
0.08 = 0.18
0.08 = 0.18
0.02 = 0.1
0.73 = 0.82
0.99 = 1.0
0.58 = 0.7
0.0 = 0.1</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 2)
 
(de price (Pr)
Line 1,696 ⟶ 4,060:
 
(for N (0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7848 0.9383 0.2292)
(prinl (price N)) )</langsyntaxhighlight>
{{out}}
Output:
<pre>0.54
0.58
Line 1,708 ⟶ 4,072:
0.38</pre>
 
=={{header|PureBasicPL/I}}==
===version 1===
<lang PureBasic>Procedure.f PriceFraction(price.f)
<syntaxhighlight lang="pl/i">declare t(20) fixed decimal (3,2) static initial (
;returns price unchanged if value is invalid
.06, .11, .16, .21, .26, .31, .36, .41, .46, .51,
Protected fraction
.56, .61, .66, .71, .76, .81, .86, .91, .96, 1.01);
Select price * 100
declare r(20) fixed decimal (3,2) static initial (
Case 0 To 5
.10, .18, .26, .32, .38, .44, .50, .54, .58, .62,
fraction = 10
.66, .70, .74, .78, .82, .86, .90, .94, .98, 1);
Case 06 To 10
declare x float, d fixed decimal (3,2);
fraction = 18
declare i fixed binary;
Case 11 To 15
fraction = 26
Case 16 To 20
fraction = 32
Case 21 To 25
fraction = 38
Case 26 To 30
fraction = 44
Case 31 To 35
fraction = 5
Case 36 To 40
fraction = 54
Case 41 To 45
fraction = 58
Case 46 To 50
fraction = 62
Case 51 To 55
fraction = 66
Case 56 To 60
fraction = 7
Case 61 To 65
fraction = 74
Case 66 To 70
fraction = 78
Case 71 To 75
fraction = 82
Case 76 To 80
fraction = 86
Case 81 To 85
fraction = 9
Case 86 To 90
fraction = 94
Case 91 To 95
fraction = 98
Case 96 To 100
fraction = 100
Default
ProcedureReturn price
EndSelect
ProcedureReturn fraction / 100
EndProcedure
 
loop:
If OpenConsole()
do i = 1 to 20;
Define x.f, i
if x < t(i) then
do; d = r(i); leave loop; end;
For i = 1 To 10
end;</syntaxhighlight>
x = Random(10000)/10000
 
PrintN(StrF(x, 4) + " -> " + StrF(PriceFraction(x), 2))
===version 2===
Next
{{trans|REXX version2}}
<syntaxhighlight lang="pl/i">cpt: Proc Options(main);
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Dcl x Dec Fixed(4,2);
Input()
Do x=0 To 1 By 0.01;
CloseConsole()
Put Edit(x,' -> ',cp(x))(Skip,f(4,2),a,f(4,2));
EndIf</lang>
End;
Sample output:
cp: Proc(p) Returns(Dec Fixed(4,2));
<pre>0.3793 -> 0.54
Dcl r(20) Dec Fixed(4,2) static init(
0.4425 -> 0.58
.10, .18, .26, .32, .38, .44, .50, .54, .58, .62,
0.0746 -> 0.18
.66, .70, .74, .78, .82, .86, .90, .94, .98, 1);
0.6918 -> 0.78
Dcl p Dec Fixed(4,2);
0.2993 -> 0.44
Dcl i Bin Fixed;
0.5486 -> 0.66
i=trunc((100*p-1)/5)+1;
0.7848 -> 0.86
Return(r(i));
0.9383 -> 0.98
End;
0.2292 -> 0.38
End;</syntaxhighlight>
0.9560 -> 1.00</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function Convert-PriceFraction
{
[CmdletBinding()]
[OutputType([double])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({$_ -ge 0.0 -and $_ -le 1.0})]
[double]
$InputObject
)
 
Process
{
foreach ($fraction in $InputObject)
{
switch ($fraction)
{
{$_ -lt 0.06} {0.10; break}
{$_ -lt 0.11} {0.18; break}
{$_ -lt 0.16} {0.26; break}
{$_ -lt 0.21} {0.32; break}
{$_ -lt 0.26} {0.38; break}
{$_ -lt 0.31} {0.44; break}
{$_ -lt 0.36} {0.50; break}
{$_ -lt 0.41} {0.54; break}
{$_ -lt 0.46} {0.58; break}
{$_ -lt 0.51} {0.62; break}
{$_ -lt 0.56} {0.66; break}
{$_ -lt 0.61} {0.70; break}
{$_ -lt 0.66} {0.74; break}
{$_ -lt 0.71} {0.78; break}
{$_ -lt 0.76} {0.82; break}
{$_ -lt 0.81} {0.86; break}
{$_ -lt 0.86} {0.90; break}
{$_ -lt 0.91} {0.94; break}
{$_ -lt 0.96} {0.98; break}
Default {1.00}
}
}
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
.7388727, .8593103, .826687, .3444635, .0491907 | Convert-PriceFraction | ForEach-Object {"{0:C}" -f $_}
</syntaxhighlight>
{{Out}}
<pre>
$0.82
$0.90
$0.90
$0.50
$0.10
</pre>
 
=={{header|Python}}==
Using the [http://docs.python.org/library/bisect.html bisect] standard module to reduce the comparisons with members of the cin array.
 
<langsyntaxhighlight lang="python">>>> import bisect
>>> _cin = [.06, .11, .16, .21, .26, .31, .36, .41, .46, .51, .56, .61, .66, .71, .76, .81, .86, .91, .96, 1.01]
>>> _cout = [.10, .18, .26, .32, .38, .44, .50, .54, .58, .62, .66, .70, .74, .78, .82, .86, .90, .94, .98, 1.00]
>>> def pricerounder(pricein):
return _cout[ bisect.bisect_right(_cin, pricein) ]</langsyntaxhighlight>
 
When dealing with money it is good to think about possible loss of precision. If we change the units to be integer cents we could use the following exact routine:
<langsyntaxhighlight lang="python">>>> import bisect
>>> _cin = [ 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96, 101]
>>> _cout = [10, 18, 26, 32, 38, 44, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 100]
>>> def centsrounder(centsin):
return _cout[ bisect.bisect_right(_cin, centsin) ]</langsyntaxhighlight>
Other options are to use the fractions or decimals modules for calculating money to a known precision.
 
<br>'''Bisection library code'''<br>
:The <code>bisect</code> Python standard library function uses the following code that improves on a simple linear scan through a sorted list:
:<langsyntaxhighlight lang="python">def bisect_right(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
 
Line 1,822 ⟶ 4,205:
if x < a[mid]: hi = mid
else: lo = mid+1
return lo</langsyntaxhighlight>
 
=={{header|Quackery}}==
This program uses the bignum rationals provided by <code>bigrat.qky</code>, so it avoids the pitfalls of storing money as floating point numbers.
<syntaxhighlight lang="quackery">[ $ 'bigrat.qky' loadfile ] now!
 
[ 2over 2over v< if 2swap 2drop ] is vmax ( n/d n/d --> n/d )
 
[ 100 1 v* 1 1 v-
0 1 vmax 5 1 v/ /
[ table
10 18 26 32 38
44 50 54 58 62
66 70 74 78 82
86 90 94 98 100 ] 100 ] is scale ( n/d --> n/d )
 
[ swap echo sp echo ] is br ( n/d --> )
 
[ 2dup br say ' --> '
scale br cr ] is test ( n/d --> )
 
0 100 test
50 100 test
65 100 test
66 100 test
100 100 test
7368 10000 test
 
( Show how to enter and display results as a decimal too. )
$ '0.7368' dup echo$
say ' --> '
$->v drop scale
2 point$ echo$</syntaxhighlight>
{{out}}
<pre>
0 100 --> 10 100
50 100 --> 62 100
65 100 --> 74 100
66 100 --> 78 100
100 100 --> 100 100
7368 10000 --> 82 100
0.7368 --> 0.82
</pre>
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang r>
price_fraction <- function(x)
{
Line 1,837 ⟶ 4,262:
#Example usage:
price_fraction(c(0, .01, 0.06, 0.25, 1)) # 0.10 0.10 0.18 0.38 1.00
</syntaxhighlight>
</lang>
 
You can extract the contents of the table as follows:
 
<syntaxhighlight lang="r">
<lang r>
dfr <- read.table(tc <- textConnection(
">= 0.00 < 0.06 := 0.10
Line 1,865 ⟶ 4,290:
breaks <- dfr$V4
values <- dfr$V6
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
 
(define table
'([0 #f]
[0.06 0.10] [0.11 0.18] [0.16 0.26] [0.21 0.32] [0.26 0.38] [0.31 0.44]
[0.36 0.50] [0.41 0.54] [0.46 0.58] [0.51 0.62] [0.56 0.66] [0.61 0.70]
[0.66 0.74] [0.71 0.78] [0.76 0.82] [0.81 0.86] [0.86 0.90] [0.91 0.94]
[0.96 0.98] [1.01 1.00])
 
;; returns #f for negatives or values >= 1.01
(define (convert x) (for/or ([c table]) (and (< x (car c)) (cadr c))))
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Simple solution, doing a linear search.<br>
Note that in Raku we don't have to worry about floating-point misrepresentations of decimals, because decimal fractions are stored as rationals.
 
{{works with|rakudo|2016.07}}
<syntaxhighlight lang="raku" line>sub price-fraction ($n where 0..1) {
when $n < 0.06 { 0.10 }
when $n < 0.11 { 0.18 }
when $n < 0.16 { 0.26 }
when $n < 0.21 { 0.32 }
when $n < 0.26 { 0.38 }
when $n < 0.31 { 0.44 }
when $n < 0.36 { 0.50 }
when $n < 0.41 { 0.54 }
when $n < 0.46 { 0.58 }
when $n < 0.51 { 0.62 }
when $n < 0.56 { 0.66 }
when $n < 0.61 { 0.70 }
when $n < 0.66 { 0.74 }
when $n < 0.71 { 0.78 }
when $n < 0.76 { 0.82 }
when $n < 0.81 { 0.86 }
when $n < 0.86 { 0.90 }
when $n < 0.91 { 0.94 }
when $n < 0.96 { 0.98 }
default { 1.00 }
}
 
while prompt("value: ") -> $value {
say price-fraction(+$value);
}</syntaxhighlight>
 
If we expect to rescale many prices, a better approach would be to build a look-up array of 101 entries.
Memory is cheap, and array indexing is blazing fast.
 
<syntaxhighlight lang="raku" line>my @price = map *.value, flat
( 0 ..^ 6 X=> 0.10),
( 6 ..^ 11 X=> 0.18),
(11 ..^ 16 X=> 0.26),
(16 ..^ 21 X=> 0.32),
(21 ..^ 26 X=> 0.38),
(26 ..^ 31 X=> 0.44),
(31 ..^ 36 X=> 0.50),
(36 ..^ 41 X=> 0.54),
(41 ..^ 46 X=> 0.58),
(46 ..^ 51 X=> 0.62),
(51 ..^ 56 X=> 0.66),
(56 ..^ 61 X=> 0.70),
(61 ..^ 66 X=> 0.74),
(66 ..^ 71 X=> 0.78),
(71 ..^ 76 X=> 0.82),
(76 ..^ 81 X=> 0.86),
(81 ..^ 86 X=> 0.90),
(86 ..^ 91 X=> 0.94),
(91 ..^ 96 X=> 0.98),
(96 ..^101 X=> 1.00),
;
 
while prompt("value: ") -> $value {
say @price[$value * 100] // "Out of range";
}</syntaxhighlight>
 
We can also build this same look-up array by parsing the table as formatted in the task description:
 
{{works with|rakudo|2016.07}}
<syntaxhighlight lang="raku" line>my $table = q:to/END/;
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
>= 0.11 < 0.16 := 0.26
>= 0.16 < 0.21 := 0.32
>= 0.21 < 0.26 := 0.38
>= 0.26 < 0.31 := 0.44
>= 0.31 < 0.36 := 0.50
>= 0.36 < 0.41 := 0.54
>= 0.41 < 0.46 := 0.58
>= 0.46 < 0.51 := 0.62
>= 0.51 < 0.56 := 0.66
>= 0.56 < 0.61 := 0.70
>= 0.61 < 0.66 := 0.74
>= 0.66 < 0.71 := 0.78
>= 0.71 < 0.76 := 0.82
>= 0.76 < 0.81 := 0.86
>= 0.81 < 0.86 := 0.90
>= 0.86 < 0.91 := 0.94
>= 0.91 < 0.96 := 0.98
>= 0.96 < 1.01 := 1.00
END
 
my @price;
 
for $table.lines {
/:s '>=' (\S+) '<' (\S+) ':=' (\S+)/;
@price[$0*100 ..^ $1*100] »=» +$2;
}
 
while prompt("value: ") -> $value {
say @price[$value * 100] // "Out of range";
}</syntaxhighlight>
 
=={{header|Raven}}==
{{trans|JavaScript}}
<syntaxhighlight lang="raven">define getScaleFactor use $v
[ 0.1 0.18 0.26 0.32 0.38 0.44 0.50 0.54 0.58 0.62 0.66 0.70 0.74 0.78 0.82 0.86 0.90 0.94 0.98 1.0 ] as $vals
$v 100 * 1 - 5 / 20 min 0 max 1 prefer dup $v "val: %g indx: %d\n" print $vals swap get
 
0 100 9 range each
100.0 / dup getScaleFactor swap "%.2g -> %.2g\n" print</syntaxhighlight>
{{out}}
<pre>0 -> 0.1
0.09 -> 0.18
0.18 -> 0.32
0.27 -> 0.44
0.36 -> 0.54
0.45 -> 0.58
0.54 -> 0.66
0.63 -> 0.74
0.72 -> 0.82
0.81 -> 0.9
0.9 -> 0.94
0.99 -> 1
</pre>
 
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX program to rescale a (decimal fraction) price (0.99 ──► 1.00).*/
<syntaxhighlight lang="rexx">/*REXX program re─scales a (decimal fraction) price (in the range of: 0¢ ──► $1). */
pad=' ' /*for inserting spaces into msg. */
pad= ' ' do j=0 to 1 by .01; if j==0 then j=0.00 /*specialfor caseinserting spaces into a message. */
do j=0 to 1 by .01 /*process the prices from 0¢ to ≤ $1 */
say pad 'original price ──►' j pad adjPrice(j) " ◄── adjusted price"
if j==0 then j= 0.00 /*handle the special case of zero cents*/
end /*j*/
exit say pad 'original price ──►' j pad adjPrice(j) " ◄── adjusted /*stick a fork in it, we're done.*/price"
end /*j*/
/*──────────────────────────────────ADJPRICE subroutine─────────────────*/
exit 0 /*stick a fork in it, we're all done. */
adjPrice: procedure; parse arg ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
select
adjPrice: procedure; parse arg ? /*a table is used to facilitate changes*/
when ?<0.06 then ?=0.10
when ?<0.11 then ?=0.18 select
when ?<0.1606 then ?= 0.2610
when ?<0.2111 then ?= 0.3218
when ?<0.2616 then ?= 0.3826
when ?<0.3121 then ?= 0.4432
when ?<0.3626 then ?= 0.5038
when ?<0.4131 then ?= 0.5444
when ?<0.4636 then ?= 0.5850
when ?<0.5141 then ?= 0.6254
when ?<0.5646 then ?= 0.6658
when ?<0.6151 then ?= 0.7062
when ?<0.6656 then ?= 0.7466
when ?<0.7161 then ?= 0.7870
when ?<0.7666 then ?= 0.8274
when ?<0.8171 then ?= 0.8678
when ?<0.8676 then ?= 0.9082
when ?<0.9181 then ?= 0.9486
when ?<0.9686 then ?= 0.9890
when ?<10.0191 then ?=1 0.0094
otherwise nop when ?<0.96 then ?= 0.98
end /*select*/ when ?<1.01 then ?= 1.00
otherwise nop
return ?</lang>
end /*select*/
'''output'''
return ?</syntaxhighlight>
<pre style="height:30ex;overflow:scroll">
{{out|output|text=:}}
<pre style="height:66ex">
original price ──► 0.00 0.10 ◄── adjusted price
original price ──► 0.01 0.10 ◄── adjusted price
Line 2,003 ⟶ 4,570:
original price ──► 0.99 1.00 ◄── adjusted price
original price ──► 1.00 1.00 ◄── adjusted price
</pre>
 
===version 2===
<syntaxhighlight lang="rexx">/* REXX ***************************************************************
* Inspired by some other solutions tested with version 1 (above)
* 20.04.2013 Walter Pachl
* 03.11.2013 -"- move r. computation (once is enough)
**********************************************************************/
rl='0.10 0.18 0.26 0.32 0.38 0.44 0.50 0.54 0.58 0.62',
'0.66 0.70 0.74 0.78 0.82 0.86 0.90 0.94 0.98 1.00'
Do i=1 To 20
Parse Var rl r.i rl
End
Do x=0 To 1 By 0.01
old=adjprice(x)
new=adjprice2(x)
If old<>new Then tag='??'
else tag=''
Say x old new tag
End
Exit
 
adjprice2: Procedure Expose r.
i=((100*arg(1)-1)%5+1)
Return r.i</syntaxhighlight>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
see pricefraction(0.5)
func pricefraction n
if n < 0.06 return 0.10 ok
if n < 0.11 return 0.18 ok
if n < 0.16 return 0.26 ok
if n < 0.21 return 0.32 ok
if n < 0.26 return 0.38 ok
if n < 0.31 return 0.44 ok
if n < 0.36 return 0.50 ok
if n < 0.41 return 0.54 ok
if n < 0.46 return 0.58 ok
if n < 0.51 return 0.62 ok
if n < 0.56 return 0.66 ok
if n < 0.61 return 0.70 ok
if n < 0.66 return 0.74 ok
if n < 0.71 return 0.78 ok
if n < 0.76 return 0.82 ok
if n < 0.81 return 0.86 ok
if n < 0.86 return 0.90 ok
if n < 0.91 return 0.94 ok
if n < 0.96 return 0.98 ok
return 1
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → input
≪ { 0.10 0.18 0.26 0.32 0.38 0.44 0.50
0.54 0.58 0.62 0.66 0.70 0.74 0.78
0.82 0.86 0.90 0.94 0.98 1 }
{ 0.06 0.11 0.16 0.21 0.26 0.31 0.36
0.41 0.46 0.51 0.56 0.61 0.66 0.71
0.76 0.81 0.86 0.91 0.96 1.01 }
1 '''WHILE''' GETI input < '''REPEAT END'''
'''IF''' DUP 1 == '''THEN''' DROP DUP SIZE '''ELSE''' 1 - '''END'''
SWAP DROP GET 2 FIX
≫ ≫ ''''PRICE'''' STO
|
'''PRICE''' ''( gross -- net ) ''
list of net values
.
.
list of thresholds
.
.
scan thresholds until passed
1 step down
forget thresholds and get net value with 2 decimals
.
|}
 
{{in}}
<pre>
0 PRICE
0.25 PRICE
0.5 PRICE
0.75 PRICE
1 PRICE
</pre>
 
{{out}}
<pre>
5: 0.10
4: 0.38
3: 0.62
2: 0.82
1: 1.00
</pre>
 
=={{header|Ruby}}==
A simple function with hardcoded values.
<langsyntaxhighlight lang="ruby">def rescale_price_fraction(value)
raise ArgumentError, "value=#{value}, must have: 0 <= value < 1.01" if value < 0 || value >= 1.01
if value < 0.06 then 0.10
Line 2,030 ⟶ 4,699:
elsif value < 1.01 then 1.00
end
end</langsyntaxhighlight>
 
Or, where we can cut and paste the textual table in one place
Line 2,037 ⟶ 4,706:
For Ruby 1.8.6, use <code>String#each_line</code>
 
<langsyntaxhighlight lang="ruby">class Price
ConversionTable = <<-END_OF_TABLE
>= 0.00 < 0.06 := 0.10
Line 2,084 ⟶ 4,753:
end
attr_reader :standard_value
end</langsyntaxhighlight>
 
And a test suite
<langsyntaxhighlight lang="ruby">require 'test/unit'
 
class PriceFractionTests < Test::Unit::TestCase
Line 2,113 ⟶ 4,782:
end
end
end</langsyntaxhighlight>
 
{{out}}
output
<pre>Loaded suite price_fraction
Started
Line 2,122 ⟶ 4,791:
 
1 tests, 22 assertions, 0 failures, 0 errors, 0 skips</pre>
Another option is using a built-in binary search:
<syntaxhighlight lang="ruby">
keys = [ 0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01]
prices = [0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62, 0.66, 0.70, 0.74, 0.78, 0.82, 0.86, 0.90, 0.94, 0.98, 1.00]
 
tests = [0.3793, 0.4425, 0.0746, 0.6918, 0.2993, 0.5486, 0.7849, 0.9383, 0.2292]
=={{header|Run BASIC}}==
tests.each do |test|
<lang runbasic>data .06, .1,.11,.18,.16,.26,.21,.32,.26,.38,.31,.44,.36,.50,.41,.54,.46,.58,.51,.62
price = prices[ keys.bsearch_index{|key| key >= test } ]
data .56,.66,.61,.70,.66,.74,.71,.78,.76,.82,.81,.86,.86,.90,.91,.94,.96,.98
puts [test, price].join(": ")
end
</syntaxhighlight>
 
=={{header|Rust}}==
dim od(100)
<syntaxhighlight lang="rust">fn fix_price(num: f64) -> f64 {
dim nd(100)
for i = 1 tomatch 19num {
0.96...1.00 => 1.00,
read oldDec
0.91...0.96 => 0.98,
read newDec
0.86...0.91 => 0.94,
j = j + 1
0.81...0.86 => 0.90,
for j = j to oldDec * 100
nd(j) 0.76...0.81 => newDec0.86,
0.71...0.76 => 0.82,
next j
0.66...0.71 => 0.78,
next i
0.61...0.66 => 0.74,
0.56...0.61 => 0.70,
0.51...0.56 => 0.66,
0.46...0.51 => 0.62,
0.41...0.46 => 0.58,
0.36...0.41 => 0.54,
0.31...0.36 => 0.50,
0.26...0.31 => 0.44,
0.21...0.26 => 0.38,
0.16...0.21 => 0.32,
0.11...0.16 => 0.26,
0.06...0.11 => 0.18,
0.00...0.06 => 0.10,
// panics on invalid value
_ => unreachable!(),
}
}
 
fn main() {
[loop]
let mut n: f64 = 0.04;
input "Gimme a number";numb
while n <= 1.00 {
decm = val(using("##",(numb mod 1) * 100))
println!("{:.2} => {}", n, fix_price(n));
print numb;" -->";nd(decm)
n += 0.04;
}
}
 
// and a unit test to check that we haven't forgotten a branch, use 'cargo test' to execute test.
goto [loop]</lang>
//
<pre>Gimme a number?12.676
// typically this could be included in the match as those check for exhaustiveness already
12.676 -->0.78
// by explicitly listing all remaining ranges / values instead of a catch-all underscore (_)
Gimme a number?4.876
// but f64::NaN, f64::INFINITY and f64::NEG_INFINITY can't be matched like this
4.876 -->0.94
#[test]
Gimme a number?34.12
fn exhaustiveness_check() {
34.12 -->0.26</pre>
let mut input_price = 0.;
while input_price <= 1. {
fix_price(input_price);
input_price += 0.01;
}
}</syntaxhighlight>
 
{{out}}
 
<pre>0.04 => 0.1
0.09 => 0.18
0.14 => 0.26
0.19 => 0.32
0.24 => 0.38
0.29 => 0.44
0.34 => 0.5
0.39 => 0.54
0.44 => 0.58
0.49 => 0.62
0.54 => 0.66
0.59 => 0.7
0.64 => 0.74
0.69 => 0.78
0.74 => 0.82
0.79 => 0.86
0.84 => 0.9
0.89 => 0.94
0.94 => 0.98
0.99 => 1</pre>
 
'''Output of unit test:'''
<pre>
running 1 test
test exhaustiveness_check ... ok
 
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def priceFraction(x:Double)=x match {
case n if n>=0 && n<0.06 => 0.10
case n if n<0.11 => 0.18
Line 2,161 ⟶ 4,893:
 
def testPriceFraction()=
for(n <- 0.00 to (1.00, 0.01)) println("%.2f %.2f".format(n, priceFraction(n)))</langsyntaxhighlight>
{{out}}
Output
<pre>
0,00 0,10
Line 2,202 ⟶ 4,934:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
const func float: computePrice (in float: x) is func
result
var float: resultprice is 0.0;
begin
if x >= 0.0 and x < 0.06 then
resultprice := 0.10;
elsif x < 0.11 then
resultprice := 0.18;
elsif x < 0.36 then
resultprice := flt(((trunc(x * 100.0) - 11) div 5) * 6 + 26) / 100.0;
elsif x < 0.96 then
resultprice := flt(((trunc(x * 100.0) - 31) div 5) * 4 + 50) / 100.0;
else
resultprice := 1.0;
end if;
end func;
Line 2,229 ⟶ 4,961:
writeln(flt(i) / 100.0 digits 2 <& " " <& computePrice(flt(i) / 100.0) digits 2);
end for;
end func;</langsyntaxhighlight>
 
The following variant of ''computePrice'' works with a table and raises RANGE_ERROR when x < 0.0 or x >= 1.01 holds:
<langsyntaxhighlight lang="seed7">const array array float: table is [] (
[] (0.06, 0.10), [] (0.11, 0.18), [] (0.16, 0.26), [] (0.21, 0.32), [] (0.26, 0.38),
[] (0.31, 0.44), [] (0.36, 0.50), [] (0.41, 0.54), [] (0.46, 0.58), [] (0.51, 0.62),
Line 2,240 ⟶ 4,972:
const func float: computePrice (in float: x) is func
result
var float: resultprice is 0.0;
local
var integer: index is 1;
Line 2,248 ⟶ 4,980:
incr(index);
end while;
resultprice := table[index][2];
else
raise RANGE_ERROR;
end if;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var table = <<'EOT'.lines.map { .words.grep{.is_numeric}.map{.to_n} }
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
>= 0.11 < 0.16 := 0.26
>= 0.16 < 0.21 := 0.32
>= 0.21 < 0.26 := 0.38
>= 0.26 < 0.31 := 0.44
>= 0.31 < 0.36 := 0.50
>= 0.36 < 0.41 := 0.54
>= 0.41 < 0.46 := 0.58
>= 0.46 < 0.51 := 0.62
>= 0.51 < 0.56 := 0.66
>= 0.56 < 0.61 := 0.70
>= 0.61 < 0.66 := 0.74
>= 0.66 < 0.71 := 0.78
>= 0.71 < 0.76 := 0.82
>= 0.76 < 0.81 := 0.86
>= 0.81 < 0.86 := 0.90
>= 0.86 < 0.91 := 0.94
>= 0.91 < 0.96 := 0.98
>= 0.96 < 1.01 := 1.00
EOT
 
func price(money) {
table.each { |row|
(row[0] <= money) ->
&& (row[1] > money) ->
&& return row[2];
}
die "Out of range";
}
 
for n in %n(0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7848 0.9383 0.2292) {
say price(n);
}</syntaxhighlight>
{{out}}
<pre>
0.54
0.58
0.18
0.78
0.44
0.66
0.86
0.98
0.38
</pre>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">"Table driven rescale"
Object subclass: PriceRescale [
|table|
Line 2,304 ⟶ 5,085:
 
"get a price"
(pr rescale: ( (Random between: 0 and: 100)/100 )) displayNl.</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">let ranges = [
(0.00..<0.06, 0.10),
(0.06..<0.11, 0.18),
(0.11..<0.16, 0.26),
(0.16..<0.21, 0.32),
(0.21..<0.26, 0.38),
(0.26..<0.31, 0.44),
(0.31..<0.36, 0.50),
(0.36..<0.41, 0.54),
(0.41..<0.46, 0.58),
(0.46..<0.51, 0.62),
(0.51..<0.56, 0.66),
(0.56..<0.61, 0.70),
(0.61..<0.66, 0.74),
(0.66..<0.71, 0.78),
(0.71..<0.76, 0.82),
(0.76..<0.81, 0.86),
(0.81..<0.86, 0.90),
(0.86..<0.91, 0.94),
(0.91..<0.96, 0.98),
(0.96..<1.01, 1.00)
]
 
func adjustDouble(_ val: Double, accordingTo ranges: [(Range<Double>, Double)]) -> Double? {
return ranges.first(where: { $0.0.contains(val) })?.1
}
 
for val in stride(from: 0.0, through: 1, by: 0.01) {
let strFmt = { String(format: "%.2f", $0) }
 
print("\(strFmt(val)) -> \(strFmt(adjustDouble(val, accordingTo: ranges) ?? val))")
}</syntaxhighlight>
 
{{out}}
 
<pre style="overflow: scroll; height: 20em">0.00 -> 0.10
0.01 -> 0.10
0.02 -> 0.10
0.03 -> 0.10
0.04 -> 0.10
0.05 -> 0.10
0.06 -> 0.18
0.07 -> 0.18
0.08 -> 0.18
0.09 -> 0.18
0.10 -> 0.18
0.11 -> 0.26
0.12 -> 0.26
0.13 -> 0.26
0.14 -> 0.26
0.15 -> 0.26
0.16 -> 0.32
0.17 -> 0.32
0.18 -> 0.32
0.19 -> 0.32
0.20 -> 0.32
0.21 -> 0.38
0.22 -> 0.38
0.23 -> 0.38
0.24 -> 0.38
0.25 -> 0.38
0.26 -> 0.44
0.27 -> 0.44
0.28 -> 0.44
0.29 -> 0.44
0.30 -> 0.44
0.31 -> 0.50
0.32 -> 0.50
0.33 -> 0.50
0.34 -> 0.50
0.35 -> 0.50
0.36 -> 0.54
0.37 -> 0.54
0.38 -> 0.54
0.39 -> 0.54
0.40 -> 0.54
0.41 -> 0.58
0.42 -> 0.58
0.43 -> 0.58
0.44 -> 0.58
0.45 -> 0.58
0.46 -> 0.62
0.47 -> 0.62
0.48 -> 0.62
0.49 -> 0.62
0.50 -> 0.62
0.51 -> 0.66
0.52 -> 0.66
0.53 -> 0.66
0.54 -> 0.66
0.55 -> 0.66
0.56 -> 0.70
0.57 -> 0.70
0.58 -> 0.70
0.59 -> 0.70
0.60 -> 0.70
0.61 -> 0.74
0.62 -> 0.74
0.63 -> 0.74
0.64 -> 0.74
0.65 -> 0.74
0.66 -> 0.78
0.67 -> 0.78
0.68 -> 0.78
0.69 -> 0.78
0.70 -> 0.78
0.71 -> 0.82
0.72 -> 0.82
0.73 -> 0.82
0.74 -> 0.82
0.75 -> 0.82
0.76 -> 0.86
0.77 -> 0.86
0.78 -> 0.86
0.79 -> 0.86
0.80 -> 0.86
0.81 -> 0.90
0.82 -> 0.90
0.83 -> 0.90
0.84 -> 0.90
0.85 -> 0.90
0.86 -> 0.94
0.87 -> 0.94
0.88 -> 0.94
0.89 -> 0.94
0.90 -> 0.94
0.91 -> 0.98
0.92 -> 0.98
0.93 -> 0.98
0.94 -> 0.98
0.95 -> 0.98
0.96 -> 1.00
0.97 -> 1.00
0.98 -> 1.00
0.99 -> 1.00
1.00 -> 1.00</pre>
 
=={{header|Tcl}}==
Structured as two functions, one to parse the input data as described in the problem into a form which Tcl can work with easily, and the other to perform the mapping.
<langsyntaxhighlight lang="tcl"># Used once to turn the table into a "nice" form
proc parseTable table {
set map {}
Line 2,330 ⟶ 5,250:
# Failed to map; return the input
return $value
}</langsyntaxhighlight>
How it is used:
<langsyntaxhighlight lang="tcl"># Make the mapping
set inputTable {
>= 0.00 < 0.06 := 0.10
Line 2,360 ⟶ 5,280:
foreach example {.7388727 .8593103 .826687 .3444635 .0491907} {
puts "$example -> [priceFraction $map $example]"
}</langsyntaxhighlight>
{{out}}
Output:
<pre>
.7388727 -> 0.82
Line 2,371 ⟶ 5,291:
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import flo
 
le = <0.06,.11,.16,.21,.26,.31,.36,.41,.46,.51,.56,.61,.66,.71,.76,.81,.86,.91,.96,1.01>
out = <0.10,.18,.26,.32,.38,.44,.50,.54,.58,.62,.66,.70,.74,.78,.82,.86,.90,.94,.98,1.>
 
price_fraction = fleq@rlPlX*|rhr\~&p(le,out)</langsyntaxhighlight>
main points:
* <code>~&p(le,out)</code> zips the pair of lists <code>le</code> and <code>out</code> into a list of pairs
Line 2,385 ⟶ 5,305:
 
test program:
<langsyntaxhighlight Ursalalang="ursala">#cast %eL
 
test = price_fraction* <0.34,0.070145,0.06,0.05,0.50214,0.56,1.,0.99,0.>
</syntaxhighlight>
</lang>
{{out}}
output:
<pre><
5.000000e-01,
Line 2,400 ⟶ 5,320:
1.000000e+00,
1.000000e-01></pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function pf(p)
If p < 0.06 Then
pf = 0.10
ElseIf p < 0.11 Then
pf = 0.18
ElseIf p < 0.16 Then
pf = 0.26
ElseIf p < 0.21 Then
pf = 0.32
ElseIf p < 0.26 Then
pf = 0.38
ElseIf p < 0.31 Then
pf = 0.44
ElseIf p < 0.36 Then
pf = 0.50
ElseIf p < 0.41 Then
pf = 0.54
ElseIf p < 0.46 Then
pf = 0.58
ElseIf p < 0.51 Then
pf = 0.62
ElseIf p < 0.56 Then
pf = 0.66
ElseIf p < 0.61 Then
pf = 0.70
ElseIf p < 0.66 Then
pf = 0.74
ElseIf p < 0.71 Then
pf = 0.78
ElseIf p < 0.76 Then
pf = 0.82
ElseIf p < 0.81 Then
pf = 0.86
ElseIf p < 0.86 Then
pf = 0.90
ElseIf p < 0.91 Then
pf = 0.94
ElseIf p < 0.96 Then
pf = 0.98
Else
pf = 1.00
End If
End Function
 
WScript.Echo pf(0.7388727)
WScript.Echo pf(0.8593103)
WScript.Echo pf(0.826687)
WScript.Echo pf(0.3444635)
</syntaxhighlight>
 
{{Out}}
<pre>
0.82
0.9
0.9
0.5
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var rescale = Fn.new { |v|
return (v < 0.06) ? 0.10 :
(v < 0.11) ? 0.18 :
(v < 0.16) ? 0.26 :
(v < 0.21) ? 0.32 :
(v < 0.26) ? 0.38 :
(v < 0.31) ? 0.44 :
(v < 0.36) ? 0.50 :
(v < 0.41) ? 0.54 :
(v < 0.46) ? 0.58 :
(v < 0.51) ? 0.62 :
(v < 0.56) ? 0.66 :
(v < 0.61) ? 0.70 :
(v < 0.66) ? 0.74 :
(v < 0.71) ? 0.78 :
(v < 0.76) ? 0.82 :
(v < 0.81) ? 0.86 :
(v < 0.86) ? 0.90 :
(v < 0.91) ? 0.94 :
(v < 0.96) ? 0.98 : 1.00
}
 
var tests = [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]
for (test in tests) {
Fmt.print("$4.2f -> $4.2f", test, rescale.call(test))
}</syntaxhighlight>
 
{{out}}
<pre>
0.49 -> 0.62
0.79 -> 0.86
1.00 -> 1.00
0.83 -> 0.90
0.99 -> 1.00
0.23 -> 0.38
0.12 -> 0.26
0.28 -> 0.44
0.72 -> 0.82
0.37 -> 0.54
0.95 -> 0.98
0.51 -> 0.66
0.43 -> 0.58
0.52 -> 0.66
0.84 -> 0.90
0.89 -> 0.94
0.48 -> 0.62
0.48 -> 0.62
0.30 -> 0.44
0.01 -> 0.10
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
func real Price(V); \Convert to standard value
Line 2,434 ⟶ 5,469:
RlOut(0, Price(0.10)); CrLf(0);
RlOut(0, Price(1.0)); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Output:
<pre>
0.18
0.18
1.00
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn convert(price){ // float --> float
// < -->, increments of 0.05 but tables are easier to update
var vert=T( T(0.06, 0.10), T(0.11, 0.18), T(0.16, 0.26),
T(0.21, 0.32), T(0.26, 0.38), T(0.31, 0.44),
T(0.36, 0.50), T(0.41, 0.54), T(0.46, 0.58),
T(0.51, 0.62), T(0.56, 0.66), T(0.61, 0.70),
T(0.66, 0.74), T(0.71, 0.78), T(0.76, 0.82),
T(0.81, 0.86), T(0.86, 0.90), T(0.91, 0.94),
T(0.96, 0.98), T(1.01, 1.00), );
vert.filter1('wrap([(a,_)]){ price<a })[1]
}</syntaxhighlight>
<syntaxhighlight lang="zkl">fcn convert2(price){ // shifting the fractional part to the integer portion
var vert=T(0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62,
0.66, 0.70, 0.74, 0.78, 0.82, 0.86, 0.90, 0.94, 0.98, 1.00);
vert[(price*100-1)/005];
}</syntaxhighlight>
<syntaxhighlight lang="zkl">T(0.7388727, 0.8593103, 0.826687, 0.3444635, 0.0491907).apply(convert) .println();
T(0.7388727, 0.8593103, 0.826687, 0.3444635, 0.0491907).apply(convert2).println();</syntaxhighlight>
{{out}}
<pre>
L(0.82,0.9,0.9,0.5,0.1)
L(0.82,0.9,0.9,0.5,0.1)
</pre>
885

edits