Converting

New to FreeBASIC? Post your questions here.
Post Reply
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Converting

Post by TurtleProgrammer »

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.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Converting

Post by srvaldez »

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
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Converting

Post by MrSwiss »

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
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Converting

Post by bcohio2001 »

What type of variables (string/double/single)? In and out?
Do you want to round the result?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Converting

Post by jj2007 »

TurtleProgrammer wrote:Is there a way to convert say: 1.019 to 1.0 and store the result in a variable?
Yes, there are several ways, but you need to know exactly how you want to convert:

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
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Converting

Post by lizard »

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
That should be exactly what you want.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Converting

Post by jj2007 »

lizard wrote:That should be exactly what you want.
So, from "say: 1.019 to 1.0" you deduct that fix() is exactly what he wants?
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Converting

Post by lizard »

Why not?
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Converting

Post by lizard »

"If you have to fix something, use fix without becoming a fixer, that fixes all."
Post Reply