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.”
-- 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)
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.
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.
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.