Price fraction: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Picat}}: Split into subsections)
m (syntax highlighting fixup automation)
Line 32:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F bisect_right(a, x)
V lo = 0
V hi = a.len
Line 49:
 
L(i) 0..10
print(‘#.2 #.2’.format(i / 10, pricerounder(i / 10)))</langsyntaxhighlight>
 
{{out}}
Line 67:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="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]
Line 103:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Price_fraction.png Screenshot from Atari 8-bit computer]
Line 131:
 
=={{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 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 177:
end loop;
end Test_Price_Fraction;
</syntaxhighlight>
</lang>
{{out}}
<div style="height: 200px;overflow:scroll">
Line 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 321:
 
printf(($"Value : "z.2dl,"Converted to standard : "z.2dl$, price, std val))
)</langsyntaxhighlight>
{{out}}
<pre>
Line 332:
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:
 
<langsyntaxhighlight lang="applescript">-- This handler just returns the standardised real value. It's up to external processes to format it for display.
 
on standardisePrice(input)
Line 366:
set output to linefeed & "Originals: " & originals & linefeed & "Standardised: " & standardised
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight 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"</langsyntaxhighlight>
 
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.
 
<langsyntaxhighlight 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)
Line 403:
set output to linefeed & "Originals: " & originals & linefeed & "Standardised: " & standardised
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight 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"</langsyntaxhighlight>
 
===Functional===
<langsyntaxhighlight lang="applescript">---------------------- PRICE FRACTION ----------------------
 
property table : [¬
Line 809:
end tell
end if
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>Price adjustments:
Line 839:
=={{header|Arturo}}==
 
<langsyntaxhighlight 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
Line 857:
loop tests 'test [
print [test "=>" getPricePoint test]
]</langsyntaxhighlight>
 
{{out}}
Line 872:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">; Submitted by MasterFocus --- http://tiny.cc/iTunis
 
Loop
Line 905:
Return 0 ; returns 0, indicating failure (shouldn't be reached though)
 
}</langsyntaxhighlight>
 
=={{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 930:
}
}
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
Line 938:
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 993:
PriceFraction! = price
END SELECT
END FUNCTION</langsyntaxhighlight>
 
{{out}} (run 5 times):
Line 1,006:
We'll use a couple of arrays for translation. Should work for several other 8-bit BASICs after converting the screen control codes.
 
<langsyntaxhighlight 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
Line 1,028:
520 next i
530 np=1:return
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,058:
=={{header|BASIC256}}==
{{trans|Gambas}}
<langsyntaxhighlight 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}
Line 1,069:
if byCount mod 5 = 0 then print
next byCount
end</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> PRINT FNpricefraction(0.5)
END
Line 1,095:
IF p < 0.91 THEN = 0.94
IF p < 0.96 THEN = 0.98
= 1.00</langsyntaxhighlight>
 
=={{header|Beads}}==
<langsyntaxhighlight Beadslang="beads">beads 1 program 'Price fraction'
 
record a_table
Line 1,134:
if v < table[ix].value
log "{v} => {table[ix].rescaled}"
exit</langsyntaxhighlight>
{{out}}
<pre>0.05 => 0.1
Line 1,145:
=={{header|Bracmat}}==
Bracmat has no native support for floating point variables nor for the fixed point values in the conversion table. Instead this solution just applies a string comparison.
<langsyntaxhighlight lang="bracmat">( ( convert
=
. ("0.06"."0.10")
Line 1,181:
& out$(!a "-->" convert$!a)
)
)</langsyntaxhighlight>
{{out}}
<pre>0.00 --> 0.10
Line 1,222:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
 
double table[][2] = {
Line 1,249:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">namespace ConsoleApplication1
{
class Program
Line 1,304:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 1,337:
return 0 ;
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,355:
 
=={{header|Clipper}}==
<langsyntaxhighlight lang="dbase">FUNCTION PriceFraction( npQuantDispensed )
LOCAL aPriceFraction := { {0,.06,.1},;
{.06,.11,.18}, ;
Line 1,387:
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 1,393:
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 1,436:
nResult := NIL
Endif
Return nResult</langsyntaxhighlight>
 
{{out}}
Line 1,477:
=={{header|Clojure}}==
{{trans|JavaScript}}
<langsyntaxhighlight 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))))</langsyntaxhighlight>
 
 
Line 1,513:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun scale (value)
(cond ((minusp value) (error "invalid value: ~A" value))
((< value 0.06) 0.10)
Line 1,535:
((< value 0.96) 0.98)
((< value 1.01) 1.00)
(t (error "invalid value: ~A" value))))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.range;
 
double priceRounder(in double price) pure nothrow
Line 1,556:
foreach (const price; [0.7388727, 0.8593103, 0.826687, 0.3444635])
price.priceRounder.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>0.82
Line 1,567:
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,599:
 
end
</syntaxhighlight>
</lang>
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
PRICE_FRACTION
Line 1,642:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,668:
 
=={{header|Elixir}}==
<langsyntaxhighlight 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},
Line 1,683:
Enum.each(val, fn x ->
:io.format "~5.2f ->~5.2f~n", [x, Price.fraction(x)]
end)</langsyntaxhighlight>
 
{{out}}
Line 1,709:
 
=={{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 1,730:
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 1,753:
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.
<langsyntaxhighlight 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]
 
Line 1,773:
[ 0.m .. 0.01m .. 1.m ]
|> Seq.ofList
|> Seq.iter (fun p -> printfn "%.2f -> %.2f" p (priceadjuster p))</langsyntaxhighlight>
{{out}}
The same as shown by Ada as of 2013-11-03T17:42Z (apart from whitespace formatting)
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">CONSTANT: dispensary-data {
{ 0.06 0.10 }
{ 0.11 0.18 }
Line 1,802:
: price-fraction ( n -- n ) dispensary-data [ first over >= ] find 2nip second ;
 
{ 0 0.5 0.65 0.66 1 } [ price-fraction ] map</langsyntaxhighlight>
 
{{out}}
Line 1,810:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Defn // to hold the three numbers from a 'row' in the table
{
Line 1,880:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Line 1,886:
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 1,897:
: 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 / ;
Line 1,908:
: test cr 101 0 ?do i print i 2 spaces normalize print cr 5 +loop ;
 
test</langsyntaxhighlight>
Output:
<pre>
Line 1,936:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program price_fraction
 
implicit none
Line 1,954:
end do
 
end program price_fraction</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang="text">0.997560 1.00
0.566825 0.70
0.965915 1.00
Line 1,965:
0.005355 0.10
0.347081 0.50
0.342244 0.50</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.050.0 Win64
 
Function rescale(price As Double) As Double
Line 2,007:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 2,035:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=87527eed297164593d88aa2c35898eaf Click this link to run this code]'''
<langsyntaxhighlight 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]
Line 2,048:
Next
 
End </langsyntaxhighlight>
Output:
<pre>
Line 2,075:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,129:
fmt.Printf("%0.4f -> %0.2f\n", v, pf(v))
}
}</langsyntaxhighlight>
 
<pre>
Line 2,145:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def priceFraction(value) {
assert value >= 0.0 && value <= 1.0
 
Line 2,161:
for (def v = 0.00; v <= 1.00; v += 0.01) {
println "$v --> ${priceFraction(v)}"
}</langsyntaxhighlight>
{{out}}
<div style="height: 200px;overflow:scroll">
Line 2,268:
 
=={{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 2,289:
| 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 2,300:
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 2,312:
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 2,326:
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="icon">
<lang Icon>
record Bounds(low,high,new)
 
Line 2,366:
}
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,386:
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 2,422:
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 2,440:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Random;
 
public class Main {
Line 2,474:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0.149969 -> 0.26
Line 2,493:
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 2,500:
 
return values[(v * 100 - 1) / 5 | 0];
}</langsyntaxhighlight>
 
=={{header|jq}}==
The solution given here is based on the JavaScript solution.
<langsyntaxhighlight 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 ] ;</langsyntaxhighlight>
The full coverage test as given in the Ada example:
<langsyntaxhighlight lang="jq">def test:
(range(0;10) | "0.0\(.) -> \( 0.01 * . | getScaleFactor)"),
(range(10;100) | "0.\(.) -> \( 0.01 * . | getScaleFactor)");
 
test</langsyntaxhighlight>
Run the test, showing the first few lines of output:
<pre>
Line 2,534:
=={{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">
<lang Julia>
const PFCUT = [6:5:101]//100
const PFVAL = [10:8:26, 32:6:50, 54:4:98, 100]//100
Line 2,550:
println(@sprintf " %.4f -> %.4f" t pricefraction(t))
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,588:
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}}
<pre>
Line 2,601:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun rescale(price: Double): Double =
Line 2,634:
if (i % 5 == 0) println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,663:
Langur uses decimal floating point.
 
<langsyntaxhighlight lang="langur"># using an implied parameter .f ...
val .pricefrac = f given .f {
case >= 0.00, < 0.06: 0.10
Line 2,693:
 
writeln .pricefrac(0.17)
writeln .pricefrac(0.71)</langsyntaxhighlight>
 
{{out}}
Line 2,700:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
dim DR(38) 'decimal range
dim PF(38) 'corresponding price fraction
Line 2,724:
next
end function
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight 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},
Line 2,748:
print("Adjusted price:", rescale(rnd))
print()
end</langsyntaxhighlight>
{{out}}
<pre>Random value: 0.61946413522022
Line 2,766:
 
=={{header|Maple}}==
<langsyntaxhighlight 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,
Line 2,783:
for i to 5 do
priceFraction (rand(0.0..1.0)());
end do;</langsyntaxhighlight>
{{out}}
<pre>0.524386 --> 0.66
Line 2,792:
 
=={{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 2,812:
 
t=0:0.001:1;
plot(t,rescale(t)); </langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight Mercurylang="mercury">:- module price.
:- interface.
:- import_module int.
Line 2,858:
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 2,875:
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}}
<pre>USER>W $$PRICFRAC^ROSETTA(.04)
Line 2,889:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,946:
end tv
return test_vals
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,958:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import random, strformat
 
# Representation of a standard value as an int (actual value * 100).
Line 2,990:
for _ in 0 .. 10:
let price = rand(1.01)
echo &"Price for {price:.2f} is {price.toStandardValue()}"</langsyntaxhighlight>
{{out}}
A random output looking something like this:
Line 3,007:
=={{header|Objeck}}==
{{trans|C#}}
<langsyntaxhighlight lang="objeck">class PriceFraction {
function : Main(args : String[]) ~ Nil {
for(i := 0; i < 5; i++;) {
Line 3,041:
return inValue;
}
}</langsyntaxhighlight>
 
{{output}}
Line 3,054:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let price_fraction v =
if v < 0.0 || v >= 1.01 then
invalid_arg "price_fraction";
Line 3,068:
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 3,087:
Printf.printf " %6g %g %b\n" v r (r = ok);
) ok_tests;
;;</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight 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
 
Line 3,097:
| i |
IN size loop: i [ f IN at(i) < ifTrue: [ OUT at(i) return ] ]
null ;</langsyntaxhighlight>
 
{{Out}}
Line 3,110:
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 3,124:
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 3,135:
);
"nasal demons"
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program PriceFraction(output);
 
const
Line 3,162:
writeln (cost:6:4, ' -> ', price[j+1]:4:2);
end;
end.</langsyntaxhighlight>
{{out}}
<pre>% ./PriceFraction
Line 3,177:
 
=={{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 3,213:
printf "%.3f -> %g\n", $m, convert($m);
}
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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;">"""
Line 3,263:
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
(
Line 3,307:
dup print 9 tochar print price_fix print nl
endfor
</syntaxhighlight>
</lang>
 
=={{header|Picat}}==
===Approach 1===
<langsyntaxhighlight Picatlang="picat">go =>
_ = random2(),
D = [
Line 3,347:
 
% Getting numbers of precision 2
frand2(N) = (random() mod N)/N.</langsyntaxhighlight>
 
{{out}}
Line 3,362:
 
===Using a fact table===
<langsyntaxhighlight Picatlang="picat">go2 =>
_ = random2(),
foreach(_ in 1..10)
Line 3,393:
r(0.86,0.91,0.94).
r(0.91,0.96,0.98).
r(0.96,1.01,1.00).</langsyntaxhighlight>
 
{{out}}
Line 3,408:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 2)
 
(de price (Pr)
Line 3,438:
 
(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}}
<pre>0.54
Line 3,452:
=={{header|PL/I}}==
===version 1===
<langsyntaxhighlight PLlang="pl/Ii">declare t(20) fixed decimal (3,2) static initial (
.06, .11, .16, .21, .26, .31, .36, .41, .46, .51,
.56, .61, .66, .71, .76, .81, .86, .91, .96, 1.01);
Line 3,465:
if x < t(i) then
do; d = r(i); leave loop; end;
end;</langsyntaxhighlight>
 
===version 2===
{{trans|REXX version2}}
<langsyntaxhighlight PLlang="pl/Ii">cpt: Proc Options(main);
Dcl x Dec Fixed(4,2);
Do x=0 To 1 By 0.01;
Line 3,483:
Return(r(i));
End;
End;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Convert-PriceFraction
{
Line 3,532:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
.7388727, .8593103, .826687, .3444635, .0491907 | Convert-PriceFraction | ForEach-Object {"{0:C}" -f $_}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,546:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.f PriceFraction(price.f)
;returns price unchanged if value is invalid
Protected fraction
Line 3,608:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>0.3793 -> 0.54
Line 3,624:
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 3,659:
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.
<langsyntaxhighlight lang="quackery">[ $ 'bigrat.qky' loadfile ] now!
 
[ 2over 2over v< if 2swap 2drop ] is vmax ( n/d n/d --> n/d )
Line 3,691:
say ' --> '
$->v drop scale
2 point$ echo$</langsyntaxhighlight>
{{out}}
<pre>
Line 3,704:
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang r>
price_fraction <- function(x)
{
Line 3,716:
#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 3,744:
breaks <- dfr$V4
values <- dfr$V6
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 3,760:
;; returns #f for negatives or values >= 1.01
(define (convert x) (for/or ([c table]) (and (< x (car c)) (cadr c))))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 3,769:
 
{{works with|rakudo|2016.07}}
<syntaxhighlight lang="raku" perl6line>sub price-fraction ($n where 0..1) {
when $n < 0.06 { 0.10 }
when $n < 0.11 { 0.18 }
Line 3,794:
while prompt("value: ") -> $value {
say price-fraction(+$value);
}</langsyntaxhighlight>
 
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" perl6line>my @price = map *.value, flat
( 0 ..^ 6 X=> 0.10),
( 6 ..^ 11 X=> 0.18),
Line 3,824:
while prompt("value: ") -> $value {
say @price[$value * 100] // "Out of range";
}</langsyntaxhighlight>
 
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" perl6line>my $table = q:to/END/;
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
Line 3,861:
while prompt("value: ") -> $value {
say @price[$value * 100] // "Out of range";
}</langsyntaxhighlight>
 
=={{header|Raven}}==
{{trans|JavaScript}}
<langsyntaxhighlight Ravenlang="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</langsyntaxhighlight>
{{out}}
<pre>0 -> 0.1
Line 3,888:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/*REXX program re─scales a (decimal fraction) price (in the range of: 0¢ ──► $1). */
pad= ' ' /*for inserting spaces into a message. */
do j=0 to 1 by .01 /*process the prices from 0¢ to ≤ $1 */
Line 3,920:
otherwise nop
end /*select*/
return ?</langsyntaxhighlight>
{{out|output|text=:}}
<pre style="height:66ex">
Line 4,027:
 
===version 2===
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* Inspired by some other solutions tested with version 1 (above)
* 20.04.2013 Walter Pachl
Line 4,048:
adjprice2: Procedure Expose r.
i=((100*arg(1)-1)%5+1)
Return r.i</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see pricefraction(0.5)
Line 4,076:
return 1
</syntaxhighlight>
</lang>
 
=={{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 4,103:
elsif value < 1.01 then 1.00
end
end</langsyntaxhighlight>
 
Or, where we can cut and paste the textual table in one place
Line 4,110:
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 4,157:
end
attr_reader :standard_value
end</langsyntaxhighlight>
 
And a test suite
<langsyntaxhighlight lang="ruby">require 'test/unit'
 
class PriceFractionTests < Test::Unit::TestCase
Line 4,186:
end
end
end</langsyntaxhighlight>
 
{{out}}
Line 4,197:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight 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
 
Line 4,216:
print numb;" -->";nd(decm)
 
goto [loop]</langsyntaxhighlight>
<pre>Gimme a number?12.676
12.676 -->0.78
Line 4,225:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn fix_price(num: f64) -> f64 {
match num {
0.96...1.00 => 1.00,
Line 4,272:
input_price += 0.01;
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,305:
 
=={{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 4,314:
 
def testPriceFraction()=
for(n <- 0.00 to (1.00, 0.01)) println("%.2f %.2f".format(n, priceFraction(n)))</langsyntaxhighlight>
{{out}}
<pre>
Line 4,355:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 4,382:
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 4,405:
raise RANGE_ERROR;
end if;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight 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
Line 4,442:
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);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,458:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">"Table driven rescale"
Object subclass: PriceRescale [
|table|
Line 4,506:
 
"get a price"
(pr rescale: ( (Random between: 0 and: 100)/100 )) displayNl.</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">let ranges = [
(0.00..<0.06, 0.10),
(0.06..<0.11, 0.18),
Line 4,541:
 
print("\(strFmt(val)) -> \(strFmt(adjustDouble(val, accordingTo: ranges) ?? val))")
}</langsyntaxhighlight>
 
{{out}}
Line 4,649:
=={{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 4,671:
# 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 4,701:
foreach example {.7388727 .8593103 .826687 .3444635 .0491907} {
puts "$example -> [priceFraction $map $example]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,714:
{{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
Line 4,732:
Return (Min(b@, 100)) ' clip top of price in accumulator
' calculate class
_Classify Param (1) : Return ((a@ - (a@>0)) / 5)</langsyntaxhighlight>
Output:
<pre>
Line 4,759:
0 OK, 0:115</pre>
=={{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 4,773:
 
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}}
<pre><
Line 4,790:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 4,808:
If n < Vin(i) Then Price_Fraction = Vout(i): Exit For
Next i
End Function</langsyntaxhighlight>
{{Out}}
<pre>0.34 := 0.5
Line 4,822:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function pf(p)
If p < 0.06 Then
Line 4,871:
WScript.Echo pf(0.826687)
WScript.Echo pf(0.3444635)
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,883:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var rescale = Fn.new { |v|
Line 4,910:
for (test in tests) {
Fmt.print("$4.2f -> $4.2f", test, rescale.call(test))
}</langsyntaxhighlight>
 
{{out}}
Line 4,937:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
func real Price(V); \Convert to standard value
Line 4,969:
RlOut(0, Price(0.10)); CrLf(0);
RlOut(0, Price(1.0)); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 4,979:
 
=={{header|zkl}}==
<langsyntaxhighlight 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),
Line 4,989:
T(0.96, 0.98), T(1.01, 1.00), );
vert.filter1('wrap([(a,_)]){ price<a })[1]
}</langsyntaxhighlight>
<langsyntaxhighlight 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];
}</langsyntaxhighlight>
<langsyntaxhighlight 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();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits