Page 1 of 1

Formatting Numbers for Arithmatic

Posted: Wed Jan 29, 2014 11:31 am
by nicoloose
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

Re: Formatting Numbers for Arithmatic

Posted: Wed Jan 29, 2014 1:23 pm
by splash21
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...

Re: Formatting Numbers for Arithmatic

Posted: Wed Jan 29, 2014 4:20 pm
by dunbarx
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

Re: Formatting Numbers for Arithmatic

Posted: Thu Jan 30, 2014 11:40 am
by nicoloose
Thank you!