Anonymous user
Price fraction: Difference between revisions
Updated to work with Nim 1.4. Done heavy restructuring and simplification, but kept the algorithm.
(Add Quackery) |
(Updated to work with Nim 1.4. Done heavy restructuring and simplification, but kept the algorithm.) |
||
Line 2,769:
=={{header|Nim}}==
<lang nim>import
# Representation of a standard value as an int (actual value * 100).
type StandardValue = distinct int
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]▼
proc `<`(a, b: StandardValue): bool {.borrow.}
# indx: 0.1-0.05->0, 0.06-0.10->1, 0.11-0.15->2, .....▼
var valu: int = toInt(f*100)▼
else:▼
# inc indx every 5 of valu, so value of 1..100 translates to indx of 0..19▼
▲const
if p < 10:▼
randomize()▼
proc toStandardValue(f: float): StandardValue =
## Convert a float to a standard value (decimal value multiplied by 100).
if value == 0: return StandardValue(10)
dec value
▲
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)
when isMainModule:
▲ randomize()
let price = rand(1.01)
echo &"Price for {price:.2f} is {price.toStandardValue()}"</lang>
{{out}}
A random output looking something like this:
<pre>Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
Price for 0.
=={{header|Objeck}}==
|