Say I have an amount of money that is automatically calculated and then rounded.
It either ends in 55.5 or 55
I need it to be 55.50 or 55.00
How do I append the decimal places and have it figure out whether it needs one 0 or two 0's at the end?
Thanks
Mike
[SOLVED] Adding one or two decimal places in an amount
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 361
- Joined: Wed Apr 27, 2011 2:12 pm
[SOLVED] Adding one or two decimal places in an amount
Last edited by karmacomposer on Tue Jul 24, 2018 3:52 pm, edited 1 time in total.
Re: Adding one or two decimal places in an amount
Hi Mike,
I always use the FORMAT() function for this:
Best
Klaus
I always use the FORMAT() function for this:
Code: Select all
...
put your_result_of_computing_numbers into tNumber
put format("%1.2f",tNumber) into tNumberWith2DecimalPlaces
## This will force two decimal places of the supplied number
...
Klaus
-
- VIP Livecode Opensource Backer
- Posts: 9582
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Adding one or two decimal places in an amount
What Klaus said.
It may also be useful to know about the "numberFormat" property. Though it is a bit old fashioned, it may be easier to grok. Again, see the dictionary.
Try this in a button script;
The extra bit about adding "0" is because the property only acts upon an arithmetical operation, not on an existing string. In general, the property is set, and then all subsequent arithmetic operations are covered. But you see that the last line does not conform to what you might want.
using "format", you would have to:
So you need to "use" the function each time you need it. The "numberFormat" property can be set sort of "globally" within a handler.
Craig Newman
It may also be useful to know about the "numberFormat" property. Though it is a bit old fashioned, it may be easier to grok. Again, see the dictionary.
Try this in a button script;
Code: Select all
on mouseUp
set the numberFormat to "#.00"
answer 55 + 0
answer 55.5 + 0
answer 55.55 + 0
put "55.5" into temp
answer temp
end mouseUp
using "format", you would have to:
Code: Select all
answer format("%1.2f","55.5")
Craig Newman
-
- Posts: 361
- Joined: Wed Apr 27, 2011 2:12 pm
Re: Adding one or two decimal places in an amount
Thank you Klaus. That worked perfectly.
Mike
Mike