Page 2 of 2
Re: Format a number to show in answer
Posted: Wed Jun 26, 2024 6:27 pm
by richmond62
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.
Re: Format a number to show in answer
Posted: Wed Jun 26, 2024 7:52 pm
by dunbarx
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
Re: Format a number to show in answer
Posted: Wed Jun 26, 2024 8:03 pm
by richmond62
I was pointing out that 'somewhere' inwith LiveCode it was possible to define number formats.
Re: Format a number to show in answer
Posted: Wed Jun 26, 2024 8:59 pm
by jacque
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.
Re: Format a number to show in answer
Posted: Wed Jun 26, 2024 9:05 pm
by jacque
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.

Re: Format a number to show in answer
Posted: Thu Jun 27, 2024 9:36 am
by richmond62
He's a very clever guy, that Mark.
He is.
But your script still goes "all funny" with decimals:
-
-
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
Re: Format a number to show in answer
Posted: Thu Jun 27, 2024 1:54 pm
by dunbarx
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
Re: Format a number to show in answer
Posted: Thu Jun 27, 2024 5:26 pm
by stam
I never miss a steak either

Re: Format a number to show in answer
Posted: Thu Jun 27, 2024 5:50 pm
by jacque
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.
Re: Format a number to show in answer
Posted: Thu Jun 27, 2024 6:17 pm
by dunbarx
Jacque.
The thing that appealed to me was the reverse step repeat which is concise and elegant.
Hmph.
My little posted stack works just fine, based on the code snippet I posted even earlier in this thread.
Craig
Re: Format a number to show in answer
Posted: Fri Jun 28, 2024 2:28 pm
by Lance
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
Re: Format a number to show in answer
Posted: Fri Jun 28, 2024 7:20 pm
by SparkOut
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