Maintaining sacond decimal place

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

Post Reply
magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Maintaining sacond decimal place

Post by magice » Sun Mar 23, 2014 3:13 pm

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?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Maintaining sacond decimal place

Post by Klaus » Sun Mar 23, 2014 3:29 pm

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 :D


Best

Klaus

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

Re: Maintaining sacond decimal place

Post by dunbarx » Sun Mar 23, 2014 3:45 pm

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

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Maintaining sacond decimal place

Post by magice » Sun Mar 23, 2014 3:51 pm

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

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

Re: Maintaining sacond decimal place

Post by dunbarx » Sun Mar 23, 2014 4:11 pm

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

Post Reply