Trying to change user input from nil into 0 so that I can do arithmetic

I want it so that if user leaves textfield blank, it will be changed before undergoing rounding so that it has an actual number to work with rather than nil. But what I think is happening is that it gets to the rounding string before the " " is turned into 0 which then causes the error. So I want to know if someone with more experience will know of an easy way to get around this. 

Also inputType for the textField is “number” if that helps.

[lua]

local function automaticCalculation (event)

if ( “submitted” == event.phase ) or (“ended” == event.phase) then

native.setKeyboardFocus( nil )

event.phase = “ended” 

if (box1P.text == “”) then

box1P.text = 0

end

box1Rounded.text = string.format ("%.2f", (math.round (((tonumber(box1P.text))))

end

end

[/lua]

Error saying doing arithmetic on nil value.

Thanks!

“” is an empty string, it’s not nil.  Perhaps what you want is:

if ( box1P.text == nil ) then

    box1P.text = 0

end

“” is an empty string, it’s not nil.  Perhaps what you want is:

if ( box1P.text == nil ) then

    box1P.text = 0

end