formated output

BlitzMax Forums/BlitzMax Beginners Area/formated output

rod54(Posted 2005) [#1]
What funcrionality does BMAX support for formatted text output ?

I.E. printing a float to 2 decimals.

d:Float = 1.3567892

What syntax would I use to print d to 2 decimal places
in BMAX ?

Hopefully Thanks for not flaming me too bad.

Rod


rod54(Posted 2005) [#2]
oops sorry for typos

that was supposed to be functionality


JazzieB(Posted 2005) [#3]
Unfortunately, none, so you have to write your own functions to achieve this. Something like this should work...

Function FormatNumber$(num#,places)
  Local output$
  output$=num:*(10^places)
  Return Left(output,output.length()-places)+"."+Right(output,places)
End Function


Untested, as I'm at work. Some of that uses the 'old' Blitz way of doing things, but should still work in BlitzMax. There may be a better way of doing it to, but that was the first that entered my head.


rod54(Posted 2005) [#4]
Will give it a try...

Thanks


JazzieB(Posted 2005) [#5]
Please note that I edited after I posted.


tonyg(Posted 2005) [#6]
There's some more discussion HERE


JazzieB(Posted 2005) [#7]
OK, that code didn't work, so here's a tested Function...

Function FormatNumber:String(num:Float,places)
	Local output:String
	output=Int(num*(10^places))
	Return Left(output,Len(output)-places)+"."+Right(output,places)
End Function


Again it uses the old string handling functions, as I couldn't get it to work with the new methods.


rod54(Posted 2005) [#8]
I was thinking of a similar idea where I was going to
basically cast it to an int by multiplying like you did and then take it back to a float and dividing it with
(10^places) would that work as well?

Thanks for the help.

Rod


rod54(Posted 2005) [#9]
Well here is my shot at trying my idea.

Function FormatNumber:Float(num:Float,places:Int)
Local in:Int
in=Int(num*(10^places))
num=Float(in/(10^places))
Return(num)
End Function

num:Float = 3.6573468

Print FormatNumber(num,2)


Results was:

Process complete
3.65000010


Guess my idea didn't work ...

Thanks for the help...


rod54(Posted 2005) [#10]
Guess that just goes to prove that the decimal and binary
systems just don't always work out evenly.


tonyg(Posted 2005) [#11]
<double-post>


tonyg(Posted 2005) [#12]
I think this is a bug. I have another couple of threads which have not had a response from BRL.
Simply...
X# = 13.12345
Y% = X*100 'Result is 1312
Z# = Y/100.0 'or *0.01. Result should be 13.12 but gives
' 13.119999
<double-post?>
<edit> to correct 100.0


xlsior(Posted 2005) [#13]
It's not a bug, it's a limitation of Floating point maths...

See my explanation in the other thread.


JazzieB(Posted 2005) [#14]
What happens if you use z:double instead of z# or z:Float?

Isn't # used to represent a single precision float to remain true with previous versions of Blitz, so using a Double should reduce the problem - although there will still be a limit to what can be stored.


tonyg(Posted 2005) [#15]

It's not a bug, it's a limitation of Floating point maths...

See my explanation in the other thread.


that B2D and B3D could cope with. See my response in the other thread.