lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Consider the answers from Diego and Paul, and I'll toss in some example
code for you:

string.format("The number is %s", tostring(data or ""))

or, along the line of Diego's suggestion:

function my_own_number_format( num )
	return type(num) == "number"
		and string.format( "The number is %d", num )
		or  "The number is " --not a number
end

my_own_number_format( nil ) --> "The number is "

//Andreas

jason zhang skrev:
> Thank you.
> How about producing a empty string? 
> 
> string.format("Number is %d",data) ==> Number is
> 
> 
> ----- Original Message ----- 
> From: "Andreas Stenius" <kaos@explosive.se>
> To: "Lua list" <lua@bazar2.conectiva.com.br>
> Sent: Thursday, August 24, 2006 4:09 PM
> Subject: Re: string.format a nil value
> 
> 
>> jason zhang skrev:
>> [snip]
>>> string.format("%d",novalue), where novalue is a nil variant.
>>>  
>>> Why not outputing a "NIL" like string? It think it's not very friendly 
>>> to print a error information and
>>> exit.
>> Because code that uses the output may rely on the facy that it should be
>> a valid number, since %d was used. If you want "nil" to come out of it,
>> i would recommend trying:
>>
>> string.format("%s", tostring(novalue))
>>
>> Which will work as expected for numbers as well (altough you'll get the
>> fraction part as well, which isn't what you'd get with %d)..
>>
>> Cheers,
>> Andreas
>>
>>