Blitz3D+ Command Reference

Str value

Parameters

value - a number, or a custom type object

Description

Converts a value into a string.

Str is a prefix operator, so Str value and Str( value ) are the same thing. It does the same conversion Blitz3D+ performs automatically when you add a number to a string, so "Score: "+n and "Score: "+Str( n ) are identical. You reach for it explicitly when you want to feed a number straight into a string command - Left( Str( n ),3 ), for instance - with nothing to concatenate it to.

Integers convert exactly as you would expect. Floats print with up to six decimal places, with trailing zeros trimmed but always at least one digit after the point: 1.5 becomes "1.5", 1000000.0 becomes "1000000.0", and a third becomes "0.333333". If you want a particular number of decimals, format it yourself - multiply, round, and reassemble with Left and Mid.

There is a handy debugging trick: Str applied to a custom type object prints all of its fields, comma separated, inside square brackets - something like [15,42,"Fluffy"]. That single line will tell you more about a misbehaving type list than a dozen Print statements.

There is no matching Val command for going the other way. Just assign the string to a numeric variable, or use Int or Float, and the conversion happens for you.

See also: Int, Float, Hex, Bin, Type.

Example

; Str Example
; -----------

; Str converts a number into a string. Blitz3D also converts
; automatically when you join a number onto a string with +.

score=1250
score_text$=Str(score)
Print "Str(1250) gives the string '"+score_text$+"' - "+Len(score_text$)+" characters"
Print ""

; Once it is a string, all the string commands work on it
Print "Zero-padded score: "+Right$("00000000"+Str(score),8)

; Floats keep their decimal part
health#=87.5
Print "Str(87.5) = '"+Str(health#)+"'"

; Joining with + converts automatically, giving the same result
Print "Automatic conversion: '"+health#+"'"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index