decimal places

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

decimal places

Post by adventuresofgreg » Tue Jan 03, 2012 7:35 pm

Hello: Is there an easy way to force LiveCode to display a number using a preset # of decimal places?

ie: convert 28799.2 to 28799.200

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: decimal places

Post by jmburnod » Tue Jan 03, 2012 8:02 pm

Hi
I'm not a specialist about this but i believe numberformat is a way to do that.

Best regards
Jean-Marc
https://alternatic.ch

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

Re: decimal places

Post by Klaus » Wed Jan 04, 2012 12:38 pm

Hi Greg,

yep, Jean-Marc is right numberformat will do!

BUT setting the numberformat will only affect arihmetic operations AFTER setting numberformat,
so you need to do something like this:
...
set numberformat to "x.xxx"
put 28799.2 into tNumber

## We FAKE an arithmetic operation!
add 0 to tNumber
answer tNumber
## -> 28799.200
...
Another way is to use FORMAT, but this is still a msytery to me :D


Best

Klaus

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: decimal places

Post by SparkOut » Wed Jan 04, 2012 3:07 pm

Numberformat will do, but to demystify ;-)

put 28799.2 into tNumber
answer format("%.3f",tNumber)

should display 28799.200

The format to use is a string "template" starting with %. Using the format template above means:
"ignore the overall length of the number because we didn't specify a length before the decimal point, we're only interested in the required precision after the point"
"precision after the decimal point to 3 decimal places because that's the number we specified after the point in the template (ie if tNumber is 28799.212345 it will be rounded to 28799.212 or 28799.21876 will be rounded to 28799.219)"
"pad the decimals with zeroes to the required precision since we used f (for floating point) indicator, not g (which is the same but ignores padding zeroes after the point)"

format returns a STRING representation of the value. (Livecode will interpret data appropriately according to what operations you are doing, so you can still do mathematical operations on the returned value, but you may need to be careful if you do other formatting, such as insert spaces and commas for 1000 separation etc.

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: decimal places

Post by adventuresofgreg » Thu Jan 05, 2012 3:34 pm

Thank you - either solution works perfectly.

greg

Post Reply