Format a number to show in answer

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Format a number to show in answer

Post by mrcoollion » Wed Jun 26, 2024 10:14 am

Hello LC specialists,
It has been a while that i posted something but I am still working with LiveCode and still love it.
However I have the next small issue and cannot seem to solve it.

I have a number e.g. 668642 and I want to show in an Answer command with a thousands seperator like 0,668,642.
I used the following code without the success i hoped for :shock:

Code: Select all

 
on mouseUp pMouseButton
   put "668642" into tNumber
   set the numberFormat to "#,###,##0."
   put (1 * tNumber) into tShowFormattedNumber // need to recalculate to effectuate set the numberFormat
   Answer "The formatted number is: " & tShowFormattedNumber
end mouseUp
This code which came the closest to what i want gives me the number 000668642 however i want it show me 0,668,642 .
How can i do this?

Regards,

MrCoolLIon (Paul)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 12:11 pm

numberFormat . . .

the Dictionary says this:
Specifies how many digits before and after the decimal point a computed number should have.
And there is NO mention of 'chopping up numbers with commas'.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 12:25 pm

Screenshot 2024-06-26 at 14.20.42.png
-

Code: Select all

on mouseUp
   ask "What's your number?"
   put it into myNUM
   put empty into fld "f1"
   repeat while myNUM is not empty
      put 1 into CYCLE
      repeat until CYCLE > 3
         put the last char of myNUM before fld "f1"
         if CYCLE = 3 then
            put "," before fld "f1"
         end if
         delete the last char of myNUM
         add 1 to CYCLE
      end repeat
   end repeat
   put 0 before fld "f1"
end mouseUp
Needs a bit of twiddling as comes up with this sort of nonsense: 0,7,823,456

Stack removed as improved version uploaded below.
Last edited by richmond62 on Wed Jun 26, 2024 12:35 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 12:34 pm

Screenshot 2024-06-26 at 14.32.46.png
-
Aaaaah: that's better:

Code: Select all

on mouseUp
   ask "What's your number?"
   put it into myNUM
   put empty into fld "f1"
   repeat while myNUM is not empty
      put 1 into CYCLE
      repeat until CYCLE > 3
         if myNUM is not empty then
         put the last char of myNUM before fld "f1"
         if CYCLE = 3 then
            put "," before fld "f1"
         end if
         end if
         delete the last char of myNUM
         add 1 to CYCLE
      end repeat
   end repeat
end mouseUp
Attachments
FORMATTER.livecode.zip
Stack
(1.07 KiB) Downloaded 125 times

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

Re: Format a number to show in answer

Post by dunbarx » Wed Jun 26, 2024 2:15 pm

Richmond.

Almost, but you have a little problem.

I wrote something like this decades ago in HC to automate writing checks, which I hated to do. The cute part of that was it "wrote out" in words in the payment amount, the worst part of check writing. I do not have that stack any longer, and just threw this together:

Anyway:

Code: Select all

on mouseUp
   ask "enter a number" with "12324567.89"
   if it is a number then
      set the itemDel to "."
      put item 1 of it into temp
      if item 2 of it <> "" then put "." & item 2 of it into decimal else put "" into decimal
      
      repeat with y = the number of chars of temp down to 1 step -3
         if y = the number of chars of temp then next repeat 
         put comma before char y + 1 of temp
      end repeat
      if char 1 of temp = comma then delete char 1 of temp
     answer temp &  decimal 
   end if
end mouseUp
Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 2:33 pm

but you have a little problem.
No, surely not. 8) What about all the other problems I have?

My main problem at the moment is that my purple tomatoes are taking an age to ripen:
-
Purple_Tomatoes.jpg

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

Re: Format a number to show in answer

Post by dunbarx » Wed Jun 26, 2024 2:47 pm

Richmond.

If you put in a number like, say, "12345.67", the answer in your posted stack gives "12,345,.67"

The HC stack I wrote in about 1990 would have also given "Twelve thousand three hundred forty five and 67/100".

It was such a pleasure to have, and it also maintained my check record book.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 2:58 pm

Craig . . .

I know, and I knocked that 'thing' together in about 10 minutes; and for 10 minutes work you get 10 minutes work. 8)

Of course I could use my stack with an "." detector somewhere in the code.

Oh, and, by-ther-way: those tomatoes were for Jacque who has a thing about my pictures. LOL

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 3:03 pm

For added 'sexiness' my stack can also produce this:
-
Screenshot 2024-06-26 at 17.01.29.png
Screenshot 2024-06-26 at 17.01.29.png (41.82 KiB) Viewed 4306 times
-
So I think I'll see if I can spare another 10 minutes. :lol:

Well, the 'leading comma' was easily swept away:

Code: Select all

on mouseUp
   ask "What's your number?"
   put it into myNUM
   put empty into fld "f1"
   repeat while myNUM is not empty
      put 1 into CYCLE
      repeat until CYCLE > 3
         if myNUM is not empty then
         put the last char of myNUM before fld "f1"
         if CYCLE = 3 then
            put "," before fld "f1"
         end if
         end if
         delete the last char of myNUM
         add 1 to CYCLE
      end repeat
   end repeat
   if the first char of fld "f1" is "," then
      delete the first char of fld "f1"
   end if
end mouseUp
-
Screenshot 2024-06-26 at 17.05.44.png
Screenshot 2024-06-26 at 17.05.44.png (41.83 KiB) Viewed 4306 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 3:12 pm

OK:

Code: Select all

on mouseUp
   ask "What's your number?"
   put it into myNUM
   put empty into fld "f1"
   repeat while myNUM is not empty
      put 1 into CYCLE
      repeat until CYCLE > 3
         if myNUM is not empty then
            put the last char of myNUM before fld "f1"
            if CYCLE = 3 then
               put "," before fld "f1"
            end if
         end if
         delete the last char of myNUM
         add 1 to CYCLE
      end repeat
   end repeat
   if the first char of fld "f1" is "," then
      delete the first char of fld "f1"
   end if
   put 1 into XXX
   repeat until char XXX of fld "f1" is empty
      put char XXX of fld "f1" into X1
      put char (XXX + 1) of fld "f1" into X2
      if X1 contains "." then
         delete char (XXX + 1) of fld "f1"
      end if
      add 1 to XXX
   end repeat
end mouseUp
-
Screenshot 2024-06-26 at 17.11.12.png
Screenshot 2024-06-26 at 17.11.12.png (41.76 KiB) Viewed 4305 times
Attachments
FORMATTER.livecode.zip
Stack.
(1.16 KiB) Downloaded 135 times

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

Re: Format a number to show in answer

Post by dunbarx » Wed Jun 26, 2024 3:32 pm

You obviously live on mars. I will not comment on the misplacement of commas and periods in the less civilized portions of our galaxy.

Craig
Last edited by dunbarx on Wed Jun 26, 2024 6:19 pm, edited 1 time in total.

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

Re: Format a number to show in answer

Post by dunbarx » Wed Jun 26, 2024 3:38 pm

Richmond, you still have a problem with it. Enter "12345.12345"

Try my stack.

Craig
Number Formatter.livecode.zip
(1.19 KiB) Downloaded 131 times

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

Re: Format a number to show in answer

Post by dunbarx » Wed Jun 26, 2024 4:14 pm

Richmond.

So with all the previous nonsense, it occurs to me that a good formatter has to ask the question whether commas and decimals are used correctly or not. 8)

if not, then simply quit the app, fold your arms and smirk.

This is not just more nonsense. In countries where the comma is used as a decimal, er, point, the use of commas to distinguish groups of three digits is prohibited. Spaces are required in such cases.

Should our efforts to make this formatter gadget be adjusted to suit? I say nay nay. As an American, and I have said this before, I expect everyone to follow what we do.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 4:20 pm

Indians chop numbers up in twos, not threes.

Over here in, erm almost Europe (Bulgaria), its the 'French' way, gaps and the comma to represent a decimal point.

However the OP was obviously going for the . . . Scottish way (invented by Napier) which the English-speaking world adopted.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Format a number to show in answer

Post by richmond62 » Wed Jun 26, 2024 6:06 pm

On my Macintosh machines, and on my Linux machines there are system-based ways to choose which numbering system floats your boat.

So, why on earth can LiveCode not just pick up the user's numbering formatting from the underlying operating system?

Currently reading a book from 1714 where there is some Mathematics: took me a while to work out that the author uses '+' as his multiplication sign. 8)

Post Reply