Formatting numbers to show commas after thousands

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
joey-f
Posts: 2
Joined: Tue Feb 03, 2015 7:45 am

Formatting numbers to show commas after thousands

Post by joey-f » Thu Apr 02, 2015 7:00 am

In a game I am coding, there is a score counter in a field. It is constantly decreasing. How do I create thousands separators for this number? I have looked everywhere and cannot find a simple answer to this seemingly simple question. (I am using version 7) For example, when I put 5,000 into the field, then decrease it to 4,999 Livecode displays it as 4999. I need commas where they are supposed to be. (This is for a school project)

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Formatting numbers to show commas after thousands

Post by Dixie » Thu Apr 02, 2015 8:16 am

Hi...

Code: Select all

on mouseUp
   put commaNumber(fld 1) into fld 1
end mouseUp

function commaNumber theString
   put the number of chars of theString into charCount
   if charCount < 3 then exit commaNumber
   
   put 0 into commaCount
   repeat with count = charCount down to 1
      
      if commaCount = 3 then
         put comma before commaNumbers
         put 0 into commaCount
      end if
      
      put char count of theString before commaNumbers
      add 1 to commaCount
   end repeat
   
   return commaNumbers
end commaNumber
Attachments
commas.livecode.zip
(1.33 KiB) Downloaded 55 times

joey-f
Posts: 2
Joined: Tue Feb 03, 2015 7:45 am

Re: Formatting numbers to show commas after thousands

Post by joey-f » Thu Apr 02, 2015 3:58 pm

Thanks Dixie! Worked perfectly!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6265
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting numbers to show commas after thousands

Post by dunbarx » Thu Apr 02, 2015 5:28 pm

Here is sort of the reverse task. I once made a check writing program. I hate to write checks, especially the part where the amount must be entered in words. In the U.S., this is actually the only formal validation of the check, not the numerical entry.

So if you have nothing to do one day, write a handler that will take, say, this: "$2,345.66"

and spit out:

"Two Thousand Three Hundred Forty Five Dollars and 66/100"

I made mine valid up to ten million. Just in case.

Craig Newman

TerryL
Posts: 69
Joined: Sat Nov 23, 2013 8:57 pm

Re: Formatting numbers to show commas after thousands

Post by TerryL » Mon Apr 06, 2015 7:35 pm

I found another way to format a number with commas and handle decimals. Do any math before comma-izing. Strip with replace command: replace comma with empty in tSubTotal --strip out commas. I can't recall the original code author. LiveCode is a flexible language. Fun stuff. Terry

Code: Select all

on mouseUp  --format number with commas
   ask "Please enter a number >= 1000." with "1234567891234.5678" titled "Format With Commas"
   if it = empty then exit to top
   put it &&"="&& MoneyFormat(it) into msg
end mouseUp

function MoneyFormat TheValue  --123456789.9876 to $123,456,789.99
   local TheDecimal
   set the numberFormat to "0.00"  --money
   put TheValue * 1 into TheValue  --calc to convert
   set the itemDel to "."
   if item -1 of TheValue <> empty then
      put item -1 of TheValue into TheDecimal
      delete item -1 of TheValue  --decimal number
   end if
   set the itemDel to ","  --default
   repeat with i = length(TheValue)-3 down to 3 step -3
      put comma after char i of TheValue
   end repeat
   set the numberFormat to "0.######"  --default
   return "$"& TheValue &"."& TheDecimal  --money
end MoneyFormat
Last edited by TerryL on Fri Apr 10, 2015 6:24 pm, edited 2 times in total.
Beginner Lab (LiveCode tutorial) and StarterKit (my public stacks)
https://tlittle72.neocities.org/info.html#26Anchor

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6265
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting numbers to show commas after thousands

Post by dunbarx » Mon Apr 06, 2015 10:24 pm

Terry.

Fun stuff indeed. I like your attitude.

Why not try your hand at the digits-to-english gadget? I had my checks put up in sheets of three so I could print them. This was before, way before, on-line banking.

Craig

Adrien
Posts: 26
Joined: Fri Jan 09, 2015 9:55 am

Re: Formatting numbers to show commas after thousands

Post by Adrien » Wed Apr 08, 2015 12:48 pm

Hi,

Don't have LC at the moment, but I would go for something like:

Code: Select all

if theNumber > 1000 then
put theNumber div 1000 & "," & theNumber mod 1000 into theOutput
else
put theNumber into theOutput
end if
just add stages or some recursive magic for millions and billions..
But doing the div/mod maths each time would probably be slower (benchmark?) than just counting the number of numbers in the number (heh).

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6265
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting numbers to show commas after thousands

Post by dunbarx » Wed Apr 08, 2015 2:21 pm

Hi.

You have to explicitly mention such terms as ""twenty" and "hundred". These need to be assembled into the money phrase nicely, since the value of your check is dependent on its accuracy and readability.

So, $2357.08 has to be "Two thousand three hundred fifty seven and 08/100"

Give it a go. All work and no play, you know...

Craig

TerryL
Posts: 69
Joined: Sat Nov 23, 2013 8:57 pm

Re: Formatting numbers to show commas after thousands

Post by TerryL » Thu Apr 09, 2015 6:33 pm

Dunbarx suggested an exercise trying to convert money to a text string, like on a check, the paper thingy people used to write on to pay for things in ancient times.

Money-to-Text conversion isn't so easy. I used the "zen" technique and visualized three elements: 1) work with hundreds, 2) use a function that calls another function, and 3) try an item list for the conversion. It came out OK after much fussing with zeros and stray spaces. I don't know how useful it is, but the item list conversion is a good trick. What, only 1 penny short of a billion? Terry

Code: Select all

on mouseUp
   local tNum
   repeat forever
      ask "Convert a money value <= $999,999,999.99 to text." with "$1,255.25" titled "Convert Money To Text"
      if it = empty then exit to top
      repeat with i = 1 to length(it)  --strip out $,commas,spaces,letters
         if char i of it is in "0123456789." then put char i of it after tNum
      end repeat
      if tNum is a number and tNum >= "0" and tNum <= "999999999.99" then exit repeat
   end repeat
   answer it &&"="&& moneyToString(tNum) with "OK"
end mouseUp

function moneyToString X  --convert $number to text string
   local tCents
   if X = empty then return "Zero Dollars And Zero Cents"
   set the numberFormat to "0.00"
   put X+0 into X  --convert to money format
   if length(X) > "12" then exit moneyToString  --max = 999999999.99
   set the itemDel to "."
   put item 2 of X into tCents
   put item 1 of X into X
   set the itemDel to ","  --back to default
   if char 1 of tCents = "0" then delete char 1 of tCents
   if tCents = "0" then put "Zero Cents" into tCents
   else put hundredToTxt(tCents) && "Cents" into tCents
   if X = "0" then return "Zero Dollars And" && tCents  --cents
   if length(X) >= "7" and length(X) <= "9" then
      put char 1 to length(X)-6 of X into M  --millions
      put char length(X)-5 to length(X)-3 of X into T  --thousands
      put char length(X)-2 to length(X) of X into H  --hundreds
      if T = "0" and H = "0" then return hundredToTxt(M) && "Million" && "Dollars And" && tCents
      else if H = "0" then return hundredToTxt(M) && "Million" && hundredToTxt(T) && "Thousand Dollars And" && tCents
      else if T = "0" then return hundredToTxt(M) && "Million" && hundredToTxt(H) && "Dollars And" && tCents
      else return hundredToTxt(M) && "Million" && hundredToTxt(T) && "Thousand" && hundredToTxt(H) && "Dollars And" && tCents
   else if length(X) >= "4" and length(X) <= "6" then
      put char 1 to length(X)-3 of X into T  --thousands
      put char length(X)-2 to length(X) of X into H  --hundreds
      if H = "0" then return hundredToTxt(T) && "Thousand Dollars And" && tCents
      else return hundredToTxt(T) && "Thousand" && hundredToTxt(H) && "Dollars And" && tCents
   else
      put char 1 to 3 of X into H  --hundreds
      if H = "1" then return hundredToTxt(H) && "Dollar And" && tCents
      else return hundredToTxt(H) && "Dollars And" && tCents
   end if
end moneyToString

function hundredToTxt Y
   local tText  --words
   if char 1 to 2 of Y = "00" then delete char 1 to 2 of Y
   else if char 1 of Y = "0" then delete char 1 of Y
   if length(Y) = "3" then  --hundreds:
      put item (char 1 of Y) of "One,Two,Three,Four,Five,Six,Seven,Eight,Nine" && "Hundred " into tText
      delete char 1 of Y
   end if
   if length(Y) = "2" then  --tens:
      if char 1 of Y = "1" then
         if char 2 of Y = "0" then put "Ten" after tText
         else put item (char 2 of Y) of "Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen" after tText
         return tText
      else
         put item (char 1 of Y) of ",Twenty-,Thirty-,Forty-,Fifty-,Sixty-,Seventy-,Eighty-,Ninety-" after tText
         delete char 1 of Y
      end if
   end if
   if length(Y) = "1" then  --ones:
      if Y = "0" and tText = empty then return empty
      else if Y = "0" and last char of tText = "-" then
         delete last char of tText
         return tText
      else put item (char 1 of Y) of "One,Two,Three,Four,Five,Six,Seven,Eight,Nine" after tText
   end if
   return tText
end hundredToTxt
Beginner Lab (LiveCode tutorial) and StarterKit (my public stacks)
https://tlittle72.neocities.org/info.html#26Anchor

sritcp
Posts: 368
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Formatting numbers to show commas after thousands

Post by sritcp » Sat Apr 18, 2015 2:23 pm

joey-f wrote:... cannot find a simple answer to this seemingly simple question.

Code: Select all

function commaTheThousands pNumber

put comma before char -3 of pNumber
if the first char of pNumber is comma then delete the first char of pNumber
return pNumber

end commaTheThousands
Regards,
Sri

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6265
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting numbers to show commas after thousands

Post by dunbarx » Mon Apr 20, 2015 4:33 pm

Terry.
like on a check, the paper thingy people used to write on to pay for things in ancient times.
Yes, grasshopper, and I wrote that little gadget around 1990, in Hypertalk, an ancient language no longer spoken much. Know, though, the code I used then would likely run in LC without a single change.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6265
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting numbers to show commas after thousands

Post by dunbarx » Tue Apr 21, 2015 4:19 am

This was the HC function. It needed only one real change, from "exit to hypercard" to "exit to top".

I changed it to be more general than the one I used to actually write my checks:

Code: Select all

function digitToWord theDigits
   put "One,Two,Three,Four,Five,Six,Seven,Eight,Nine" into ones
   put "Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen" into teens
   put ",Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety" into tens
   set numberformat to "#.00"
    if "."is not in theDigits then put ".00" after theDigits
      get offset(".",theDigits)
      if it > 7 then
            answer "You can't afford to write this check"
            exit to top
      end if
   get offset(".",theDigits)
   if char it + 1 of theDigits = "" then put "00" after theDigits
   if it <> 0 then put " Dollars and" && char it + 1 to it + 2 of theDigits & "/100" into temp
   delete char it to it + 2 of theDigits
   get number(chars of theDigits)
   if char it - 1 of theDigits = 1 then put item char it of theDigits + 1 of teens before temp
   else
      put item char it of theDigits of ones before temp
      put item char it - 1 of theDigits of tens & space before temp
   end if
   
   if it >= 3 and char it - 2 of theDigits <> 0 then put item char it - 2 of theDigits of ones && "Hundred" & space before temp
   if it = 4 then put item char it - 3 of theDigits of ones && "Thousand" & space before temp
   if it = 5 and char 1 of theDigits = 1 then put item (char it - 3 of theDigits + 1) of teens  && "Thousand" & space before temp
   else if it = 5 then put item char it - 4 of theDigits of tens && item char it - 3 of theDigits of ones && "Thousand" & space before temp
   if it = 6 then put item char 1 of theDigits of ones && "Hundred" && item char 2 of theDigits of tens && item char 3 of theDigits of ones & " Thousand "  before temp
   
   return temp
end digitToWord
Craig

pmilera
Posts: 1
Joined: Tue Jun 16, 2015 2:32 am

Re: Formatting numbers to show commas after thousands

Post by pmilera » Tue Jun 16, 2015 2:44 am

This is an easy fix too.

local memhold,ToCalc,ticker,x

on mouseUp
--(put the number you want to add comma to into memhold)
set the itemdelimiter to "."
put item 1 of memhold into ToCalc
if the number of chars in ToCalc < "3" then
exit to top
end if
put the number of chars in ToCalc into ticker
repeat for each char x in ticker
subtract 3 from ticker
put "," after char ticker of ToCalc
end repeat
put ToCalc into item 1 of memhold

answer memhold -- (here it is converted)
end mouseUp

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”