Page 1 of 1
Maintaining sacond decimal place
Posted: Sun Mar 23, 2014 3:13 pm
by magice
I am working on an app that requires tracking money values. The problem is, when I add whole dollar amounts in variables, it loses the second decimal place money format. E.G. 10.00 + 1.00 = 11
I was hoping for a simple "Excel-Like" solution such as a field property. Unfortunately it doesn't seem to be that simple. Before I write out a script to check decimal position and rewrite the variable after the mathematical operations, is there a built in LiveCode solution?
Re: Maintaining sacond decimal place
Posted: Sun Mar 23, 2014 3:29 pm
by Klaus
Hi magice,
you could use the FORMAT() function:
...
put format("%.2f",11+10)
...
-> 21.00
"%.2f" -> forces the expression/number supplied in the second parameter to be formatted
as a FLOATING point number with precision 2, which is what you are looking for
Best
Klaus
Re: Maintaining sacond decimal place
Posted: Sun Mar 23, 2014 3:45 pm
by dunbarx
Hi.
What Klaus said.
Another way is to set the "numberFormat" property:
Code: Select all
on mouseUp
set the numberformat to "0.00"
answer 10.00 + 1.00
answer 10 + 1 --What do you know? Forces two decimals even when not called for
end mouseUp
Note that this property is, by default, set to six decimal places globally within LC. So without doing anything, in a new session, say, if you:
Code: Select all
on mouseUp
answer 77 / 23
end mouseUp
You would get an answer with six decimals. You can get up to about 16 if you need it. Please read up on this property in the dictionary.
But the way LC handles whole integers, as you have discovered, does require some sort of management to get the display you want.
Craig Newman
Re: Maintaining sacond decimal place
Posted: Sun Mar 23, 2014 3:51 pm
by magice
Thank you Klaus & Craig
I was working on something like this:
Code: Select all
function moneyFormat tAmount
put the number of characters in tAmount -1 into tC
if character tC of tAmount is "." then put tAmount & "0" into tAmount
if tAmount contains "."
then
return tAmount
else
put tAmount & ".00" into tAmount
return tAmount
end if
end moneyFormat
Your solutions are much cleaner.
Thank you
Re: Maintaining sacond decimal place
Posted: Sun Mar 23, 2014 4:11 pm
by dunbarx
Ah,
But your efforts are so worth doing. Building strings from whole cloth is terrific for learning the byways of LC. Anyone can use the "numberFormat" property. Only experts bend and forge the language like you did.
Craig