--- title: tonumber summary: Converts a valid value to a number or returns nil; an explicit base applies to string input. section: reference kind: api status: reference updated: 2026-10-26 tags: ["API","X4"] sources: ["https://wiki.egosoft.com/X4%20Foundations%20Wiki/Modding%20Support/","https://www.lua.org/manual/5.1/manual.html","https://luajit.org/ext_ffi_api.html"] --- ## Return value | Type | Meaning | | --- | --- | | [`number`](wiki:type-number) \| [`nil`](wiki:type-nil) | Types describe the declared interface, not proof of a successful game effect. | [Types and methods](wiki:types) ## Parameters | Name | Type | | --- | --- | | `value` | [`any`](wiki:type-results) | | `base?` | [`number`](wiki:type-number) | ? marks an optional argument; ... means a variable number of arguments. Invalid input can raise a Lua error rather than return nil. ## Example and context ```lua local amount = tonumber("12") if amount ~= nil then amount = amount + 1 end ``` **What happens:** amount = 13 after converting "12" and adding 1. This fragment does not print or write a log message. The values shown are the expected results of the example. See [language basics](wiki:x4-code-basics), [types and methods](wiki:types) and [Lua / MD events](wiki:md-lua-bridge). Examples there are independent learning examples. No project source files or private implementation fragments are distributed with this reference.