How can I remove a comma from a string ?

Posted by Gedeon on Mon 29 Oct 2018 11:58 PM — 4 posts, 19,840 views.

#0
o.k so I am trying to remove a comma from the string "110,000"
to leave me with the string "110000" that I can the convert to number like this :


-- a is a variable with value "110,000" and is variable type "string"

a = "110,000"

-- the what I need to know here

-- then convert the new value of a that should now be "110000" as a variable that is type "string" to a variable that is type "number"

a = tonumber(a)



I know that I could use the built in g.sub() function like this :


print(string.gsub("110,000", ",", ""))


But that gives me the count of how many commas are removed at the end of the ouput which I don't want in the variable, also I need my code to be abloe to convert millions and billions aswell so what I'm looking for would be something that can convert like this :


100,000 to 100000
1,000,000 to 1000000
1,000,000,000 to 1000000000


Thanks.
Australia Forum Administrator #1
All you have to do is use string.gsub to remove the comma. It returns two values, one is the converted string, and the other is the count of conversions.

You can discard the count by assigning the result to a variable, like this:


a = string.gsub("110,000", ",", "")
b = tonumber (a)
print (b)



Or you can do it one one line, the extra brackets around the string.gsub call force the extra return value to be discarded:


a = tonumber ((string.gsub("110,000", ",", "")))
print (b)





Also, please don't post the same question twice. Edit your question if you want to modify it.
#2
Thank You! Sweet as a nut... I can now have my trigger and send like this :


Trigger : Ayla blesses you with * additional gold.

Send : 

a = string.gsub("%1", ",", "")
b = tonumber(a)
print(b)



... to keep track of my gold from daily blessings in Aardwolf with a counter setup that adds the value of variable b! thank you! :D

Also, I appologise for the double post, I did try to edit it but I think it made a new post for some reason, also to be even fairer, I spent about 10 minutes trying to figure out how to delete the old one!!! ARGH! Won't happen again :) *halo*

So, my whole send looks like this to keep track of the gold from daily blessings with the counter included :


a = string.gsub("%1", ",", "")
b = tonumber(a)

counter_db_bonus_gold = tonumber(GetVariable("counter_db_bonus_gold"))
if counter_db_bonus_gold == nil then
    SetVariable("counter_db_bonus_gold", "0")
    counter_db_bonus_gold = tonumber(GetVariable("counter_db_bonus_gold"))
end
counter_db_bonus_gold = tonumber(GetVariable("counter_db_bonus_gold"))
counter_db_bonus_gold = counter_db_bonus_gold + b
SetVariable("counter_db_bonus_gold", counter_db_bonus_gold)


Bless You ! Thank you sir.

How would I go about RE-formatting 100 k's, millions and billions?

Thanks.

- Gedeon
Amended on Tue 30 Oct 2018 03:53 PM by Gedeon
USA Global Moderator #3
Sorry for the delayed response.

Quote:
How would I go about RE-formatting 100 k's, millions and billions?

I don't understand the question.


But maybe you're looking for something like the convert_numbers_to_words function in lua/words_to_numbers.lua?


require "words_to_numbers"
print(convert_numbers_to_words("94921277802687490518"))

prints
Quote:

ninety four quintillion nine hundred twenty one quadrillion two hundred seventy seven trillion eight hundred two billion six hundred eighty seven million four hundred ninety thousand five hundred eighteen