Converting

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

Converting

Postby TurtleProgrammer » Jul 11, 2018 19:00

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: 1629
Joined: Sep 25, 2005 21:54

Re: Converting

Postby srvaldez » Jul 11, 2018 19:16

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

Re: Converting

Postby MrSwiss » Jul 11, 2018 19:48

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

Re: Converting

Postby bcohio2001 » Jul 11, 2018 22:12

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

Re: Converting

Postby jj2007 » Jul 12, 2018 5:23

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

Re: Converting

Postby lizard » Jul 12, 2018 7:25

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: 767
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Converting

Postby jj2007 » Jul 12, 2018 7:54

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

Re: Converting

Postby lizard » Jul 12, 2018 8:04

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

Re: Converting

Postby lizard » Jul 12, 2018 10:00

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

Return to “Beginners”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 2 guests