Price fraction: Difference between revisions

 
(15 intermediate revisions by 10 users not shown)
Line 933:
 
=={{header|BASIC}}==
 
{{works with|QBasic}}
 
This could also be done by building an array, but I felt that this was simpler.
 
Line 1,002 ⟶ 1,000:
.0491907 .1
 
==={{header|Commodore BASICBASIC256}}===
{{trans|Gambas}}
<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
DEF FNpricefraction(p)
IF p < 0.06 THEN = 0.10
IF p < 0.11 THEN = 0.18
IF p < 0.16 THEN = 0.26
IF p < 0.21 THEN = 0.32
IF p < 0.26 THEN = 0.38
IF p < 0.31 THEN = 0.44
IF p < 0.36 THEN = 0.50
IF p < 0.41 THEN = 0.54
IF p < 0.46 THEN = 0.58
IF p < 0.51 THEN = 0.62
IF p < 0.56 THEN = 0.66
IF p < 0.61 THEN = 0.70
IF p < 0.66 THEN = 0.74
IF p < 0.71 THEN = 0.78
IF p < 0.76 THEN = 0.82
IF p < 0.81 THEN = 0.86
IF p < 0.86 THEN = 0.90
IF p < 0.91 THEN = 0.94
IF p < 0.96 THEN = 0.98
= 1.00</syntaxhighlight>
 
==={{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.
 
Line 1,056 ⟶ 1,094:
ready.</pre>
 
==={{header|BASIC256FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.050.0 Win64
{{trans|Gambas}}
<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}
 
Function rescale(price As Double) As Double
for byCount = 1 to 100
If price < 0.00 OrElse price > 1.00 Then Return price
for byCheck = 0 to byLimit[?]
Select Case price
if byCount < byLimit[byCheck] then exit for
Case Is < 0.06 : Return 0.10
next byCheck
Case Is < 0.11 : Return 0.18
print ljust((byCount/100),4," "); " -> "; ljust((byValue[byCheck]/100),4," "); chr(9);
ifCase byCountIs mod 5 =< 0.16 then: printReturn 0.26
Case Is < 0.21 : Return 0.32
next byCount
Case Is < 0.26 : Return 0.38
end</syntaxhighlight>
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
=={{header|BBC BASIC}}==
Dim d As Double = i/100.0
<syntaxhighlight lang="bbcbasic"> PRINT FNpricefraction(0.5)
Print Using "#.##"; d;
END
Print " -> ";
Print Using "#.##"; rescale(d);
DEF FNpricefraction(p)
Print " ";
IF p < 0.06 THEN = 0.10
If i Mod 5 IF p <= 0.11 THENThen = 0.18Print
Next
IF p < 0.16 THEN = 0.26
 
IF p < 0.21 THEN = 0.32
Print
IF p < 0.26 THEN = 0.38
Print "Press any key to quit"
IF p < 0.31 THEN = 0.44
Sleep</syntaxhighlight>
IF p < 0.36 THEN = 0.50
 
IF p < 0.41 THEN = 0.54
{{out}}
IF p < 0.46 THEN = 0.58
<pre>
IF p < 0.51 THEN = 0.62
0.01 -> 0.10 0.02 -> IF0.10 p <0.03 -> 0.5610 THEN =0.04 -> 0.10 0.05 -> 0.6610
0.06 -> 0.18 0.07 -> IF0.18 p <0.08 -> 0.6118 THEN =0.09 -> 0.18 0.10 -> 0.7018
0.11 -> 0.26 0.12 -> IF0.26 p <0.13 -> 0.6626 THEN =0.14 -> 0.26 0.15 -> 0.7426
0.16 -> 0.32 0.17 -> IF0.32 p <0.18 -> 0.7132 THEN =0.19 -> 0.32 0.20 -> 0.7832
0.21 -> 0.38 0.22 -> IF0.38 p <0.23 -> 0.7638 THEN =0.24 -> 0.38 0.25 -> 0.8238
0.26 -> 0.44 0.27 -> IF0.44 p <0.28 -> 0.8144 THEN =0.29 -> 0.44 0.30 -> 0.8644
0.31 -> 0.50 0.32 -> IF0.50 p <0.33 -> 0.8650 THEN =0.34 -> 0.50 0.35 -> 0.9050
0.36 -> 0.54 0.37 -> IF0.54 p <0.38 -> 0.9154 THEN =0.39 -> 0.54 0.40 -> 0.9454
0.41 -> 0.58 0.42 -> IF0.58 p <0.43 -> 0.9658 THEN =0.44 -> 0.58 0.45 -> 0.9858
0.46 -> 0.62 0.47 -> 0.62 0.48 -> 0.62 0.49 -> 0.62 0.50 -> 0.62
= 1.00</syntaxhighlight>
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}}==
Line 1,144 ⟶ 1,547:
 
=={{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.
<syntaxhighlight lang="bracmat">( ( convert
=
Line 1,562 ⟶ 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}}==
Line 1,665 ⟶ 2,276:
Given: 0.95 Adjusted:0.98
</pre>
 
 
=={{header|Elixir}}==
Line 1,967 ⟶ 2,577:
0.342244 0.50</syntaxhighlight>
 
=={{header|FreeBASICFutureBasic}}==
<syntaxhighlight lang="freebasicfuturebasic">' FB 1.050.0 Win64
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
Function rescale(price As Double) As Double
NSUInteger i
If price < 0.00 OrElse price > 1.00 Then Return price
Select Case price
for i = 1 to 100
Case Is < 0.06 : Return 0.10
Casedouble Isd <= 0i/100.11 : Return 0.18
printf @"%.2f -> %.2f\t\b", d, fn PriceFraction( d )
Case Is < 0.16 : Return 0.26
Caseif Isi <mod 0.215 : Return== 0.32 then print
next
Case Is < 0.26 : Return 0.38
end fn
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
 
fn GetPriceFractions
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
 
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
HandleEvents
{{out}}
</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|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|Go}}==
Line 2,663 ⟶ 3,236:
Langur uses decimal floating point.
 
<syntaxhighlight lang="langur">#val using.pricefrac an= impliedfn(.f) parameter{ switch[and] .f ...{
val .pricefrac = f given .f {
case >= 0.00, < 0.06: 0.10
case >= 0.06, < 0.11: 0.18
Line 2,686 ⟶ 3,258:
case >= 0.96, <= 1.00: 1.00
default: throw "bad data"
}}
 
# The default operator between test cases is "and".
# That is, writing "case" without a logical operator is the same as writing "case and".
# To make a given case act as a switch case does in other languages, use "case or".
}
 
writeln .pricefrac(0.17)
Line 2,698 ⟶ 3,266:
<pre>0.32
0.82</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|Lua}}==
Line 2,764 ⟶ 3,305:
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}}==
Line 2,863 ⟶ 3,484:
 
<syntaxhighlight lang="mercury">adjust(Cents) = array.lookup(price_table, Cents).</syntaxhighlight>
 
 
=={{header|MUMPS}}==
Line 3,544 ⟶ 4,166:
$0.10
</pre>
 
=={{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|Python}}==
Line 4,092 ⟶ 4,638:
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
|
'''PRICEqPRICE''' ''( gross -- net ) ''
list of net values
.
Line 4,245 ⟶ 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|
<syntaxhighlight 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
dim od(100)
</syntaxhighlight>
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|Rust}}==
Line 4,761 ⟶ 5,290:
</pre>
 
=={{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|Ursala}}==
<syntaxhighlight lang="ursala">#import flo
Line 4,838 ⟶ 5,320:
1.000000e+00,
1.000000e-01></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|VBScript}}==
Line 4,933 ⟶ 5,383:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var rescale = Fn.new { |v|
885

edits