Price fraction: Difference between revisions
Content added Content deleted
(Add NetRexx implementation) |
m (Added Nimrod code) |
||
Line 1,499: | Line 1,499: | ||
0.62 0.74 |
0.62 0.74 |
||
</pre> |
</pre> |
||
=={{header|Nimrod}}== |
|||
<lang nimrod>import strutils, math |
|||
const |
|||
pricemap: array[0 .. 19, int] = [10,18,26,32,38,44,50,54,58,62,66,70,74,78,82,86,90,94,98,100] |
|||
# outputs an int (=>float*100) |
|||
proc floatToPrice100(f: float): int = |
|||
# indx: 0.1-0.05->0, 0.06-0.10->1, 0.11-0.15->2, ..... |
|||
var valu: int = toInt(f*100) |
|||
if valu == 0: |
|||
result = 10 |
|||
else: |
|||
dec(valu) |
|||
# inc indx every 5 of valu, so value of 1..100 translates to indx of 0..19 |
|||
var indx: int = 2*int(valu/10)+int((valu%%10)/5) |
|||
result = pricemap[indx] |
|||
# str representation of an int (that is a representation of a float price) |
|||
proc price100ToStr(p: int): string = |
|||
if p < 10: |
|||
result = "0.0" & $p |
|||
if p < 100: |
|||
result = "0." & $p |
|||
else: |
|||
result = "1.00" |
|||
randomize() |
|||
var i: int = 0 |
|||
for x in 0 .. 10: |
|||
i = random(101) |
|||
echo("Price for ", i.price100ToStr(), ", is: ", float(i/100).floatToPrice100().price100ToStr())</lang> |
|||
{{out}} |
|||
A random output something like: |
|||
<pre>Price for 0.73, is: 0.82 |
|||
Price for 0.29, is: 0.44 |
|||
Price for 0.25, is: 0.38 |
|||
Price for 0.52, is: 0.66 |
|||
Price for 0.66, is: 0.78 |
|||
Price for 0.23, is: 0.38 |
|||
Price for 0.62, is: 0.74 |
|||
Price for 0.26, is: 0.44 |
|||
Price for 0.70, is: 0.78 |
|||
Price for 0.69, is: 0.78 |
|||
Price for 0.39, is: 0.54</pre> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |