Formatting Numbers for Arithmatic

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
nicoloose
Posts: 99
Joined: Mon Sep 16, 2013 3:35 pm

Formatting Numbers for Arithmatic

Post by nicoloose » Wed Jan 29, 2014 11:31 am

I am currently fetching my data from my database like this:

select format(sum(numberField),2) as numberField from table

Which returns a number formatted like this : 1,234.56.

Livecode does not seem to like it when I try and add two numbers with this formatting together. I have looked at selecting the data without formatting but am then having trouble formatting inside LiveCode. I was always used to using numberFormat to format my data but this does not seem to work either.

Can anyone suggest the best way for me to get the formatting I require.

Thanks.
Nic

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Formatting Numbers for Arithmatic

Post by splash21 » Wed Jan 29, 2014 1:23 pm

Here's a lunch-at-the-keyboard function - I think it works OK :wink:

Code: Select all

function nFormat pVal
   local tInt, tDec, tLength, tIndex
   set itemDel to "."
   put item -1 of pVal into tDec
   delete item -1 of pVal
   put length(pVal) into tLength
   repeat with tIndex = (tLength div 3) * 3 to 3 step -3
      if tIndex < tLength then put comma before char -tIndex of pVal
   end repeat
   return pVal & "." & tDec
end nFormat

1234.56 => 1,234.56
...etc...
LiveCode Development & Training : http://splash21.com

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

Re: Formatting Numbers for Arithmatic

Post by dunbarx » Wed Jan 29, 2014 4:20 pm

Hi.

Why not just replace comma with empty in the number?

Are there other characters that will break an arithmetic operation as well, like "$"? If so, you need to lose them too.

Craig Newman

nicoloose
Posts: 99
Joined: Mon Sep 16, 2013 3:35 pm

Re: Formatting Numbers for Arithmatic

Post by nicoloose » Thu Jan 30, 2014 11:40 am

Thank you!

Post Reply