Converting
-
- Posts: 37
- Joined: Jan 26, 2017 7:54
Converting
Is there a way to convert say: 1.019 to 1.0 and store the result in a variable? I found PRINT USING, but of course that prints to the screen which I don't want to do.
Re: Converting
use format
Code: Select all
#include "string.bi"
dim as double x=1.019
dim as string s
s=format(x,"##.0")
print s
sleep
Re: Converting
Double to INT-type conversion (without any string):
Code: Select all
' numeric version
Dim As Double d = 1.019
Dim As Long l = fix(d)
Print l, d
Sleep
-
- Posts: 556
- Joined: Mar 10, 2007 15:44
- Location: Ohio, USA
- Contact:
Re: Converting
What type of variables (string/double/single)? In and out?
Do you want to round the result?
Do you want to round the result?
Re: Converting
Yes, there are several ways, but you need to know exactly how you want to convert:TurtleProgrammer wrote:Is there a way to convert say: 1.019 to 1.0 and store the result in a variable?
Code: Select all
#include "crt\math.bi"
dim as double numd
dim as integer someint
print "fix", "int", "floor", "ceil", "double"
for numd=-1.2 to 1.2 step 0.1
print fix(numd), int(numd), floor(numd), ceil(numd), numd
next
' the conversions
someint=fix(numd)
someint=int(numd)
someint=floor(numd)
someint=ceil(numd)
sleep()
Code: Select all
fix int floor ceil double
-1 -2 -2 -1 -1.2
-1 -2 -2 -1 -1.1
-0 -1 -1 -0 -0.9999999999999999
-0 -1 -1 -0 -0.8999999999999999
-0 -1 -1 -0 -0.7999999999999999
-0 -1 -1 -0 -0.7
-0 -1 -1 -0 -0.6
-0 -1 -1 -0 -0.5
-0 -1 -1 -0 -0.4
-0 -1 -1 -0 -0.3
-0 -1 -1 -0 -0.2
-0 -1 -1 -0 -0.1
-0 -1 -1 -0 -2.775557561562891e-017
0 0 0 1 0.09999999999999998
0 0 0 1 0.2
0 0 0 1 0.3
0 0 0 1 0.4
0 0 0 1 0.5
0 0 0 1 0.6
0 0 0 1 0.7
0 0 0 1 0.7999999999999999
0 0 0 1 0.8999999999999999
0 0 0 1 0.9999999999999999
1 1 1 2 1.1
1 1 1 2 1.2
Re: Converting
TurtleProgrammer wrote:Is there a way to convert say: 1.019 to 1.0 and store the result in a variable? I found PRINT USING, but of course that prints to the screen which I don't want to do.
Code: Select all
dim as single x = 1.019, i
i = fix(x)
'print i
Re: Converting
So, from "say: 1.019 to 1.0" you deduct that fix() is exactly what he wants?lizard wrote:That should be exactly what you want.
Re: Converting
Why not?
Re: Converting
"If you have to fix something, use fix without becoming a fixer, that fixes all."