Jump to content

Getting the number of decimal places: Difference between revisions

Added various BASIC dialects (BASIC256, Chipmunk Basic, GW-BASIC, MSX Basic, QBasic, QB64 and Yabasic) Moved FreeBASIC to section BASIC
m (→‎{{header|Phix}}: use pygments)
(Added various BASIC dialects (BASIC256, Chipmunk Basic, GW-BASIC, MSX Basic, QBasic, QB64 and Yabasic) Moved FreeBASIC to section BASIC)
Line 204:
12.3450 has 4 decimals
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">arraybase 1
dim n[5]
n[1] = 7
n[2] = 12.00
n[3] = 12.345
n[4] = 12.345677
n[5] = 0.142857142857142
 
for i = n[?,] to n[?]
print n[i] + " has " + dec(n[i]) + " decimals"
next i
end
 
function dec(n)
c = string(n)
if instr(c, ".") then return length(mid(c, instr(c, ".") + 1, length(c))) else return 0
end function</syntaxhighlight>
{{out}}
<pre>7 has 0 decimals
12.0 has 1 decimals
12.345 has 3 decimals
12.345677 has 6 decimals
0.14285714286 has 11 decimals</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|Yabasic}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 dim n(4)
110 data 7,12,12.345,12.345677,0.142857
120 for i = 0 to 4
130 read n(i)
140 print n(i);" has ";dek(n(i));" decimals"
150 next i
160 end
170 sub dek(n)
180 c$ = str$(n)
190 if instr(c$,".") then dek = len(mid$(c$,instr(c$,".")+1)) else dek = 0
200 end sub</syntaxhighlight>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vbnet">Function dec(n As Double) As Uinteger
Dim As String c = Str(n)
Return Iif(Instr(c, "."), Len(Mid(c,Instr(c, ".")+1)), 0)
End Function
 
Dim As Double n(1 To ...) => {7, 12.00, 12.345, 12.345677, 0.142857142857142}
 
For i As Integer = 1 To Ubound(n)
Print n(i); " has "; dec(n(i)); " decimals"
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 7 has 0 decimals
12 has 0 decimals
12.345 has 3 decimals
12.345677 has 6 decimals
0.142857142857142 has 15 decimals</pre>
 
==={{header|GW-BASIC}}===
The [[#MSX Basic|MSX Basic]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|QBasic}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">100 DIM N(4)
110 DATA 7,12,12.345,12.345677,0.142857
120 FOR I = 0 TO 4
130 READ N(I)
140 GOSUB 180
150 PRINT N(I);"has";DEC;"decimals"
160 NEXT I
170 END
180 'sub dec(n)
190 C$ = STR$(N(I))
200 IF INSTR(C$,".") <> 0 THEN DEC = LEN(MID$(C$,INSTR(C$,".")+1)) ELSE DEC = 0
210 RETURN</syntaxhighlight>
{{out}}
<pre> 7 has 0 decimals
12 has 0 decimals
12.345 has 3 decimals
12.34568 has 5 decimals
.142857 has 6 decimals</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
The [[#MSX Basic|MSX Basic]] solution works without any changes.
 
==={{header|QB64}}===
The [[#MSX Basic|MSX Basic]] solution works without any changes.
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">dim n(4)
data 7,12.00,12.345,12.345677,0.142857142857142
for i = 0 to 4
read n(i)
print n(i), " has ", dek(n(i)), " decimals"
next i
end
 
sub dek(n)
c$ = str$(n)
if instr(c$, ".") then return len(mid$(c$, instr(c$, ".") + 1)) else return 0: fi
end sub</syntaxhighlight>
{{out}}
<pre>7 has 0 decimals
12 has 0 decimals
12.345 has 3 decimals
12.3457 has 4 decimals
0.142857 has 6 decimals</pre>
 
=={{header|C}}==
Line 316 ⟶ 433:
5
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Function dec(n As Double) As Uinteger
Dim As String c = Str(n)
Return Iif(Instr(c, "."), Len(Mid(c,Instr(c, ".")+1)), 0)
End Function
 
Dim As Double n(1 To ...) => {7, 12.00, 12.345, 12.345677, 0.142857142857142}
 
For i As Integer = 1 To Ubound(n)
Print n(i); " has "; dec(n(i)); " decimals"
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 7 has 0 decimals
12 has 0 decimals
12.345 has 3 decimals
12.345677 has 6 decimals
0.142857142857142 has 15 decimals</pre>
 
=={{header|Go}}==
2,167

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.