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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Really dumb question i guess

Really dumb question i guess

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


Posted by AquaBrother   Brazil  (19 posts)  Bio
Date Tue 11 Jun 2013 02:09 AM (UTC)
Message
Hi guys, i've been trying to do something which i think might be pretty easy to solve but yet i'm strugling to do it...

Here we go, i have a number for example:

1500700

Thing is i wanted to make something in order to separate that with dots to output something like:

1.500.700 (like a normal number separation)

I was trying to work with string.gsub and string.sub together and alone but i coundnt think on something to solve that...

You guys are my last hope cuz i give up.

Thx in advance.
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 11 Jun 2013 04:47 AM (UTC)
Message
There is an inbuilt module "commas" that does that:


require "commas"

print (commas ("1500700"))


Result:


1,500,700


You could edit that to change the comma to a dot, or do a string replace, eg.


require "commas"

print ((string.gsub (commas ("1500700"), ",", ".")))


Output:


1.500.700

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 11 Jun 2013 04:48 AM (UTC)
Message
The function itself is pretty small:


--[[

Commas in numbers

This function adds commas to big numbers. 
For example 123456 becomes "123,456".

--]]

-- ----------------------------------------------------------

function commas (num)
  assert (type (num) == "number" or
          type (num) == "string")
  
  local result = ""

  -- split number into 3 parts, eg. -1234.545e22
  -- sign = + or -
  -- before = 1234
  -- after = .545e22

  local sign, before, after =
    string.match (tostring (num), "^([%+%-]?)(%d*)(%.?.*)$")

  -- pull out batches of 3 digits from the end, put a comma before them

  while string.len (before) > 3 do
    result = "," .. string.sub (before, -3, -1) .. result
    before = string.sub (before, 1, -4)  -- remove last 3 digits
  end -- while

  -- we want the original sign, any left-over digits, the comma part,
  -- and the stuff after the decimal point, if any
  return sign .. before .. result .. after

end -- function commas


In the middle change the comma to a period if you want a different separator.

- Nick Gammon

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

Posted by AquaBrother   Brazil  (19 posts)  Bio
Date Reply #3 on Tue 11 Jun 2013 02:01 PM (UTC)

Amended on Tue 11 Jun 2013 02:02 PM (UTC) by AquaBrother

Message
I knew there was a magical command, rofl...

Thx again Nick, you're awesome...

Really appreciate the help...

Best regards...
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,598 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.