print using

Blitz3D Forums/Blitz3D Programming/print using

barryem(Posted 2004) [#1]
Is there a print using function or some way to limit the number of decimal places that the text function shows? I realize I can convert to a string and truncate but I'm hoping there's a more direct way.

Thanks,
Barry


wizzlefish(Posted 2004) [#2]
The Command Reference is your friend. If not in the command reference, maybe an external DLL.


barryem(Posted 2004) [#3]
Of course I looked in the command reference but I didn't find anything there. I've thought about a DLL for this with a using() function that returns a formatted string but I'd like to avoid that while I'm just learning Blitz3D. I'm a retired programmer and but I'm trying to keep things nice and simple.

Barry


Damien Sturdy(Posted 2004) [#4]
then the easiest is to do it yourself, surely?


WolRon(Posted 2004) [#5]
There is no formatting in Blitz. You have to write your own functions. I know because I just wrote a program that does the same thing.

Here is some code that I wrote that formats any integer or float to a specified number of digits/decimals and fills leading or trailing spaces with zeros or blanks. It also only adds the decimal point if decimal digits are requested.

Usage: Print "Dollars $" + Digit$(3, 2, variable#, True, False)
or Text "Z = " + Digit$(6, 6, variable#, True)
or Print "Integer value = " + Digit$(2, 0, variable#)

intdigits = number of digits before the decimal point
decdigits = number of digits after the decimal point
number# = the number you want formatted
zeros = (optional) fill spaces before/after decimal point with zeros or 'blanks' (defaults to 'blanks')
leadingzeros = (optional) set to false if you don't want zeros before the decimal point (defaults to leading zeros)

Global blank$ = " " ;or blank$ = "" or whatever you choose

Function Digit$(intdigits, decdigits, number#, zeros=False, leadingzeros=True)
	If Sgn(number#) >= 0
		intnum = Floor(number#)
	Else
		intnum = Ceil(number#)
		sign = True
	EndIf
	integer$ = intnum
	If integer$ = "0"
		integer$ = ""
	Else
		sign = False
	EndIf
	decnum# = number# * 10000
	dec$ = decnum#
	dot = Instr(dec$, ".", 1)
	dec$ = Left$(dec$, dot - 1)
	dec$ = Right$(dec$, 4)
	While Len(dec$) < 4
		dec$ = "0" + dec$
	Wend
	For iter = intdigits To 1 Step -1
		If integer$ <> ""
			newstr$ = Right$(integer$, 1) + newstr$
		Else
			If sign = True
				newstr$ = "-" + newstr$
				sign = False
				iter = iter - 1
			EndIf
			If (zeros = False Or leadingzeros = False) And iter > 0
				newstr$ = blank$ + newstr$
			Else
				newstr$ = "0" + newstr$
			EndIf
		EndIf
		integer$ = Left$(integer$, Len(integer$) -1)
	Next
	If decdigits > 0
		newstr$ = newstr$ + "."
		For iter = 1 To decdigits
			If dec$ <> ""
				newstr$ = newstr$ + Left$(dec$, 1)
			Else
				If zeros = False
					newstr$ = newstr$ + blank$
				Else
					newstr$ = newstr$ + "0"
				EndIf
			EndIf
			dec$ = Right$(dec$, Len(dec$) - 1)
		Next
	EndIf
	Return newstr$
End Function


The code above was used specifically for converting numbers to usually 4 decimal digits. It may require tweaking for whatever your purposes are.

Note that there is what looks like some 'additional' code at the beginning of the function to try to obtain the integer and decimal parts of the number. This was necessary to compensate for Blitz's habit of rounding the last couple of significant digits.


skidracer(Posted 2004) [#6]
http://www.blitzbasic.com/codearcs/codearcs.php?code=47


barryem(Posted 2004) [#7]
I posted this by mistake as a reply in the code archive but I had intended to post it here.

This might be a little simpler and a little faster depending on the internals of the compiler

Call it with either:
print using$(number, decimals)
or
text x,y, using$(number, decimals)

; n# is the number to convert
; dec is the number of decimal places wanted

Function using$(n#, dec)
s$ = Str$ n#
t$ = Left$(s$, Len(s$) - Instr(s$, ".")+ dec -1)
Return t$
End Function

Does anyone know the internals of Blitz3D enough to know if this sort of thing is faster or slower than calling a function in a DLL, assuming the function in the DLL is itself fairly effecient?

Or, maybe more to the point, is Blitz3D fast enough with little stuff like this that we don't have to care most of the time?

Barry


John Blackledge(Posted 2004) [#8]
If you're an old-time programmer like me, then I can appreciate your concern - on some early computer systems string handling was a real no-no.
But the PC + Blitz is fast. Let me give you an example:
a) If my character collides with a mesh/terrain
b) I get the entity handle
c) get the surface
d) get the brush
e) get the texture name
f) parse the texture name for one of 10 reference strings _within_ the name
g) if so, search a complementary array to get a wave file name/handle (pre-loaded)
h) and play it (footsteps).
Obviously, the footsteps are actually executed dependant on a timer (walking/running), but the actual test is done every frame!
Fast enough for you?


barryem(Posted 2004) [#9]
I started programming in the mid-60s so I guess I'm an old time programmer. But my teachers were older time programmers. :)

The scenario you describe is too general to be much use in judging speed. It seems fast and most of the stuff I'm likely to be doing will be fairly simple so yes it probably is fast enough for me.

My question was mostly one of curiosity. Not whether Blitz is fast enough but about which way is faster and what are Blitz's strengths and weaknesses. Whether calling a DLL is fast enough to justify it's use is kind of an important question. It'll help me decide if I want to get into doing DLLs or not. I'd rather not.

My main interest in Blitz is as a toy to play with. I'm retired with much time on my hands. I'm old enough and my brain has slowed enough that I don't want to do any serious programming. But it's nice to be able to play at a little non-serious programming now and then. I don't have any need to be efficient but efficiency is enough of a habit in me that it'll be less fun if I know I'm doing things badly. I think I'm going to be lazy enough not to measure that for myself, so I asked. :)

Barry


bradford6(Posted 2004) [#10]
whichever is easiest, is usually the best answer. the less time you spend fudging with decimal spaces, the more time to make more meaningful code.