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

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

Re: Format a number to show in answer

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

Here:

https://lessons.livecode.com/m/4068/l/3 ... n-a-column
In the Pattern property, type this pattern:

### ### ##0.00,### ### ##0.00[red]

Note the pattern is possibily divided in three items:

- the first for positive numbers

- the second for negative numbers

- the last for Zero values

For more information about how to build a formatting pattern, please follow this lesson:

http://lessons.runrev.com/m/4068/l/28533

In the Thousands Separtor property, type a comma.
That URL leads to the wrong destination.

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 7:52 pm

Richmond.

What were you trying to say in your last post? Had it something to do with Richard's comment that certain new field text-parsing properties might be very useful?

Craig

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

Re: Format a number to show in answer

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

I was pointing out that 'somewhere' inwith LiveCode it was possible to define number formats.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Format a number to show in answer

Post by jacque » Wed Jun 26, 2024 8:59 pm

Mark Wieder posted this to the mailing list ages ago and I stole it. This is for processing (US) money, but should work for any decimal number with minor modification:

Code: Select all

FUNCTION fNumericToMoney pNumeric
    local tMoney
    local x
    
    -- leading dollar sign and two digits for cents
    put format("$%.2f", pNumeric) into tMoney
    -- add the commas as necessary
    -- 4 is to ensure we don't get a comma before the dollar sign
    -- 6 here is skipping over the first three digits, the period, and the cents
    REPEAT with x=length(tMoney)-6 to 4 step -3
        -- +1 to skip over the dollar sign
        put comma before char x+1 of tMoney
    END REPEAT
    return tMoney
END fNumericToMoney
I only needed the integer part, so I condensed it to this:

Code: Select all

function addCommas 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 addCommas
He's a very clever guy, that Mark.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Format a number to show in answer

Post by jacque » Wed Jun 26, 2024 9:05 pm

richmond62 wrote:
Wed Jun 26, 2024 2:58 pm
Oh, and, by-ther-way: those tomatoes were for Jacque who has a thing about my pictures. LOL
I've learned to live with it. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Format a number to show in answer

Post by richmond62 » Thu Jun 27, 2024 9:36 am

He's a very clever guy, that Mark.
He is.

But your script still goes "all funny" with decimals:
-
SShot 2024-06-27 at 11.45.14.png
-

Code: Select all

on mouseUp
   ask "Enter a number"
   put it into XXX
   put addCommas(XXX) into fld "f1"
end mouseUp

function addCommas 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 addCommas

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 » Thu Jun 27, 2024 1:54 pm

Ahem.

My little posted stack works just fine, based on the code snippet I posted even earlier in this thread.

I know this because I never make misteaks.

Here it is again. Sheesh.

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

stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Re: Format a number to show in answer

Post by stam » Thu Jun 27, 2024 5:26 pm

I never miss a steak either ;)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Format a number to show in answer

Post by jacque » Thu Jun 27, 2024 5:50 pm

richmond62 wrote:
Thu Jun 27, 2024 9:36 am
But your script still goes "all funny" with decimals:
Yes it does. Like I said, I only needed integers so I modified Mark's handler to omit decimal calculations. His was intended only for US money but with some adjustments could be used for any decimal number. The thing that appealed to me was the reverse step repeat which is concise and elegant.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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 » Thu Jun 27, 2024 6:17 pm

Jacque.
The thing that appealed to me was the reverse step repeat which is concise and elegant.
Hmph. :cry:
My little posted stack works just fine, based on the code snippet I posted even earlier in this thread.
8)

Craig

Lance
Posts: 28
Joined: Sat Sep 05, 2020 2:36 pm

Re: Format a number to show in answer

Post by Lance » Fri Jun 28, 2024 2:28 pm

Sort of related to this thread. I created a field with number formatting and I added it to my object library so I can place the object on any new card I am working on. Think it works pretty well and I am in US, so that is what I use it for. So thought I would share it, may be helpful to some.

Lance
Attachments
formatted field example.zip
(1.42 KiB) Downloaded 123 times

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Format a number to show in answer

Post by SparkOut » Fri Jun 28, 2024 7:20 pm

Just to add some alternatives...
On Windows you can use VBScript FormatNumber to use the format of the system preferences, with some additional options

Code: Select all

   put random (1000) * 1000 into tN
   put "result = FormatNumber(" & tN & ",0)" into tScript --the zero forces zero decimal places, you can see https://www.w3schools.com/asp/func_formatnumber.asp for options
   do tScript as vbscript
   put the result --into some container
On Linux you could get some something useful by getting the result from shell with something like (not tested)

Code: Select all

put random (1000) * 1000 into tN
put "cprintf(" & quote & "%'d" & quote & "," & tN & ")" into tShell
get shell tShell
put it --into some container
A for Mac, I dare say there's a call that could be made in similar fashion, but don't know

Post Reply