Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Tips and tricks ➜ How can I remove a comma from a string ?

How can I remove a comma from a string ?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Gedeon   (4 posts)  Bio
Date Mon 29 Oct 2018 11:58 PM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 30 Oct 2018 05:52 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Gedeon   (4 posts)  Bio
Date Reply #2 on Tue 30 Oct 2018 03:34 PM (UTC)

Amended on Tue 30 Oct 2018 03:53 PM (UTC) by Gedeon

Message
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
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #3 on Sat 03 Nov 2018 12:40 AM (UTC)

Amended on Sat 03 Nov 2018 01:01 AM (UTC) by Fiendish

Message
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

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,001 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.