Removing $ from string

Im trying to make a number animation and for that I need the start number of the animation which is in the text label.

print(textLabel.Text) -- prints "$54"

I want to tonumber() the 54 only problem is that for that I need to remove the $ sign. But idk how, because the gsub keeps giving errors for using $ signs. Like: PlayerGui.BiddingGui.BiddingLocal:10: invalid argument #2 to ‘tonumber’ (base out of range).

These are the lines I tried:

local startPrice = tonumber(string.gsub(priceText.Text, "%$", ""))

And:

local startPrice = tonumber(string.gsub(priceText.Text, "$", "")) -- removing the % sign
2 Likes

you don’t need to use tonumber, the price will display just fine
also, i am pretty sure it should be $%, rather than %$

1 Like

Running string.gsub("$54", "%$", "") correctly returns “54” for me. It’s possible that you had a missing / misplaced comma or bracket.

1 Like

%$ is correct.

2 Likes

When I do:

print(string.gsub(priceText.Text,"%$",""))

it prints “101 1” while the text was “$101”

1 Like

I just realised the issue as you were replying - gsub returns 2 values, not just 1, which was messing it up.

This abomination seems to get it working, but I’ll try to get a nicer bit of code for it:

print(tonumber(({string.gsub("$54", "%$", "")})[1]))

if you’re willing to spread it out over multiple lines, you can do something like this:

local Text, _ = string.gsub(priceText.Text, "%$", "")
local startPrice = tonumber(Text)

Another way to do it in 1 line would to be use string.sub to remove the first character in the text

1 Like

A better way would be to just wrap it in parentheses, which will force exactly one value.

print((string.gsub("$500", "%$", ""))) --> "500"
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.