Easy way to add commas to a number

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MichaelBluejay
Posts: 234
Joined: Thu Jul 01, 2010 11:50 am

Easy way to add commas to a number

Post by MichaelBluejay » Tue Mar 17, 2020 6:56 am

All the algos I could find for adding commas to numbers were either long, or complicated, or both. I hate long and/or complicated code. So here's a short and simple way to add commas to a number.

Code: Select all

put 1234567.89 into val

put floor(val) into floored // Get the number w/o the decimals
put val - floored into savedDecimals
if the number of chars in floored >3 then put comma before char -3 of floored
if the number of chars in floored >7 then put comma before char -7 of floored
put char 2 to -1 of savedDecimals after floored

put floored
That's it, five lines of code, no loops, no fuss, no mess.
With an additional line of code you can have it handle numbers larger than 999,999,999, if you're using numbers that big.
It does fail on negative numbers without extra code; that code is in the fuller function below.

I would point out that despite LC's marketing slogan of "less code", in most other languages I can insert commas with a *single* line of code, but because LC lacks backreferences in regular expressions, it takes several lines to do it in LC.

Here's a complete function which adds commas, formats to two decimal places, and handles negative numbers properly.

Code: Select all

put c(1234567.89)

function c val
   if char 1 of val is "-" then
      put "-" into savedSign
      delete char 1 of val
   end if
   put round(val*100)/100 into val
   put floor(val) into floored // Get the number w/o the decimals
   put val - floored into savedDecimals
   if savedDecimals is 0 then put "0.00" into savedDecimals
   else if length(savedDecimals) = 3 then put "0" after savedDecimals
   if the number of chars in floored >3 then put comma before char -3 of floored
   if the number of chars in floored >7 then put comma before char -7 of floored
   put char 2 to -1 of savedDecimals after floored
   put savedSign before floored
   return floored
end c
Edited 3/17/20 to add note and fix for negative numbers.
Last edited by MichaelBluejay on Tue Mar 17, 2020 7:00 pm, edited 1 time in total.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easy way to add commas to a number

Post by bogs » Tue Mar 17, 2020 8:03 am

...here's a short and simple way to add commas to a number.
Erm, I might be wrong, or even just not awake yet, but isn't that what 'format' or 'numberFormat' is for?

Edit* - and what is wrong with loops?

Code: Select all

on mouseUp
   repeat until the number of characters of item 1 of field 1 =< 3
      put comma before char -3 of item 1 of field 1
   end repeat
   if the first char of field 1 is comma then delete the first char of field 1 --<-- this is a just in case error checker, and probably isn't needed...
end mouseUp
4 lines of code, 3 if you drop the probably unnecessary error check deletion, and could probably be turned into a nice function as well, i.e. I see you have a 2 place decimal number, and we wouldn't want to put an ugly comma before the period, so you could do something like...

Code: Select all

on callingHandler
set the itemDelimiter to "."
commaThis(item 1 of field "x")
end callingHandler

function commaThis pNum
    set the itemDelimiter to comma
    repeat until the number of characters of item 1 of field 1 =< 3
      put comma before char -3 of item 1 of pNum
   end repeat
   return pNum
end commaThis
By the way, the above code was a modified version from a post by Sri, thank you Sri :D
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Easy way to add commas to a number

Post by dunbarx » Tue Mar 17, 2020 1:46 pm

Hi.

I am with Bogs here, in that the simple loop he posted is, for me, far more sensible than any other method. By "sensible" I don't mean non nonsensical, but rather I can "see" the intent of the code. I can see the parsing, in each turn of the loop, of the items as they build. I can see how the items themselves sort of self-parse.

A loop is the way to make sense of this process. To force it into a series of explicit steps seems unseemly.

Craig

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Easy way to add commas to a number

Post by Klaus » Tue Mar 17, 2020 1:53 pm

Here an not so old thread about this topic with some solutions:
https://forums.livecode.com/viewtopic.php?f=68&t=33736

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easy way to add commas to a number

Post by bogs » Tue Mar 17, 2020 2:03 pm

I saw the point of that link right away, Klaus !
Quick links FAQ bogs Private messages Notifications
Board index
Information

You are not authorised to read this forum.
:twisted: :twisted:
Image

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Easy way to add commas to a number

Post by Klaus » Tue Mar 17, 2020 2:08 pm

Oh, ah, yes, sorry, forgot that this is not allowed for everyone... :?

OK, here some solutions, I am sure the authors will not mind.
1. -hh wrote
Using as few math as possible:

Code: Select all

function separateWithComma pNum
  replace comma with empty in pNum
  put length(pNum) div 3 into tN
  if length(pNum) mod 3 = 0 then subtract 1 from tN
  repeat with i=1 to tN
    put comma after char -4*i of pNum
  end repeat
  return pNum
end separateWithComma
2. Jaques wrote:
So many ways to do things in LC. Here's a variation based on a function by Mark Wieder:

Code: Select all

function addComma pNum
  -- add the commas as necessary
  repeat with x=length(pNum)-3 to 3 step -3
    put comma before char x+1 of pNum
  end repeat
  return pNum
end addComma
3. Thierry wrote:
Hi,
Another road much less travelled...

Code: Select all

sunnYreplace( N, "([0-9])(?=(0{3})+$)", "\1,", newN)
and in action:

Code: Select all

   repeat for each item N in "100,1000,10000,100000,1000000"
      get sunnYreplace( N, "([0-9])(?=(0{3})+$)", "\1,", newN)
      put N & " -> " & newN &cr after T
   end repeat
   put T
---- >
100 -> 100
1000 -> 1,000
10000 -> 10,000
100000 -> 100,000
1000000 -> 1,000,000
And Sparkout added a link to this forum:
viewtopic.php?p=12696#p12696

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easy way to add commas to a number

Post by bogs » Tue Mar 17, 2020 2:19 pm

Ah thanks!

I've actually come across the solution Jacque posted based on Mark's function, which is probably the shortest other one I saw. Thierry's solution looks fascinating too ! I wish I had a brain that functioned like that :(
Image

Post Reply