Type 'string' could not be converted to number. Error constantly popping up

Hello!
I am trying to make a number formatting system with a highly trusted Format Number library made by someone else. However, when I put in my amount into the function shown below (which is a number) it says: “Type ‘string’ could not be converted to number.”

Code:

Please provide any help possible.
Thanks! :smiley:

1 Like

Can you show the FormatCompact function?

You can force a function to return something (I think) by doing this:

function FormatCompact(variables) : number

end
2 Likes

The function:-

-- Configuration
-- The suffixes for abbreviation in every power of thousands.
local COMPACT_SUFFIX = {
	"K", "M", "B", "T"
}
local CACHED_SKELETON_SETTINGS = true
--

local MainAPI = require(script.Parent.Main)
local FormatNumberSimpleAPI = { }

local SKELETON_CACHE = if CACHED_SKELETON_SETTINGS then { } else nil
local COMPACT_SKELETON_CACHE = if CACHED_SKELETON_SETTINGS then { } else nil

function FormatNumberSimpleAPI.Format(value: number, skeleton: string?): string
	local success
	local formatter = nil

	assert(type(value) == "number", "Value provided must be a number")

	if skeleton == nil then
		skeleton = ""
	end
	assert(type(skeleton) == "string", "Skeleton provided must be a string")

	if CACHED_SKELETON_SETTINGS then
		formatter = SKELETON_CACHE[skeleton]
	end

	if not formatter then
		success, formatter =
			MainAPI.NumberFormatter.forSkeleton(skeleton)
		assert(success, formatter :: string)

		if CACHED_SKELETON_SETTINGS then
			SKELETON_CACHE[skeleton] = formatter
		end
	end

	return (formatter :: MainAPI.NumberFormatter):Format(value)
end

function FormatNumberSimpleAPI.FormatCompact(value: number, skeleton: string?): string
	local success
	local formatter = nil

	assert(type(value) == "number", "Value provided must be a number")

	if skeleton == nil then
		skeleton = ""
	end
	assert(type(skeleton) == "string", "Skeleton provided must be a string")

	if CACHED_SKELETON_SETTINGS then
		formatter = COMPACT_SKELETON_CACHE[skeleton]
	end

	if not formatter then
		success, formatter =
			MainAPI.NumberFormatter.forSkeleton(skeleton)
		assert(success, formatter :: string)

		formatter = (formatter :: MainAPI.NumberFormatter)
			:Notation(MainAPI.Notation.compactWithSuffixThousands(COMPACT_SUFFIX))

		if CACHED_SKELETON_SETTINGS then
			COMPACT_SKELETON_CACHE[skeleton] = formatter
		end
	end

	assert(#COMPACT_SUFFIX ~= 0, "Please provide the suffix abbreviations for FormatCompact at the top of the Simple ModuleScript")

	return formatter:Format(value)
end

return table.freeze(FormatNumberSimpleAPI)

The whole script I meant*
The script with the function*

I think the Luau linter is confused.
Try using a different variable name for the return type. I.e.

local formattedAmount = FormatNumber.FormatCompact(amount)

EDIT: This is not a bug it is working correctly.

Looks to be the issue.

You’re setting amount as a string in your original code posted because the :string makes it a string instead of a number.

(Sorry for the poor wording, if there is any, I’m somewhat new to type checking)

2 Likes

The function is not the first “.Format” function but the “FormatCompact” one.

Both functions posted return a string.

If you rely on that functionality, you can possibly silence these errors by doing
amount = tonumber(FormatNumber.FormatCompact(amount))

I was going to recommend this, but I was unable to find any documentation for the library @kry1068 is using. This looks like one of those formatters that condenses numbers to the nearest unit (ie. 100,000,000 becomes 100m), so this may not work if that’s the case.

1 Like


This is what happens now.

So I should just remove the “:string”?

Do you rely on :string being there? I’m not sure how your system works so I didn’t suggest that.

See my latest reply. To verify it, would you mind printing amount on the line before you declare the amount? That would ultimately tell us what’s causing the type mismatch.

2 Likes

I will send you a link to the documentation quickly. Hold on a second.

1 Like

Before declaring the amount, I printed the amount and it returned ‘nil’.

The issue is the reassignment.

You declare amount as a number, then are telling Luau to set it to the return value of FormatCompact, which is not a number.

Your code will work fine if you do this:

local amountText = FormatNumber.FormatCompact(amount) -- Don't need any type hints here

-- set the text to amountText
1 Like

So I don’t need to declare amount as a number type. Correct?

You should keep amount being declared as number. You can’t reassign it to a string, you have to make a separate variable for the return of FormatCompact.

local function UpdateCurrency(currency: "Seeds" | "Crystal", amount: number)
    local amountText = FormatNumber.FormatCompact(amount)

So I did this earlier but it still would send an error. Not that the string could not be changed to a number but that the value must be a number. It was sending the error from the script which defined the format number function.