formated output

BlitzMax Forums/BlitzMax Beginners Area/formated output

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

oops sorry for typos

that was supposed to be functionality

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.

Will give it a try...

Thanks

Please note that I edited after I posted.

There's some more discussion HERE

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.

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

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...

Guess that just goes to prove that the decimal and binary
systems just don't always work out evenly.

<double-post>

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

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

See my explanation in the other thread.

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.


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.