Number formats

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andrew99
Posts: 11
Joined: Mon Mar 23, 2009 12:49 pm

Number formats

Post by andrew99 » Mon Mar 23, 2009 1:02 pm

Hi,

I'm new to Revolution so appologies if this is rather simple question.

I have a number of fields to which i'm outputing values for display. These need to display a certain number format - some need to display a currency format ie: £XX,XXX and others need to display a percentage XX% format.

Any ideas how? I was hoping there might have been a number format option in the GUI like Excel but I can't find one so I assume I need to include some code specific to each field.

Thanks in advance of any assistance.

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

Post by SparkOut » Mon Mar 23, 2009 4:09 pm

Hi Andrew,
This thread http://forums.runrev.com/phpBB2/viewtopic.php?t=2690 shows a few options you have when it comes to number formats. Your situation doesn't need to have a special bit of code for each field though - this is the beauty of Rev's wonderful "virtual properties" feature. It's a subject which has cropped up on the forum a few times lately, so as an indication of how they can be handled in a "real world" situation, these are some (rudimentary) handlers to give an idea. If you put these setProp handlers in the message path somewhere - probably stack level or perhaps a backscript - then instead of "putting a value into the field" you can "set the property of the field" where the setProp handler will process the property and put the appropriately adjusted value into the field for you. These handlers do not "pass" the setting of the property, so the fields do not actually get assigned a custom property, but the adjusted value is put into the field for display, so it's a "virtual" property, if you follow.

Code: Select all

setProp cDisplayCurrency pValue
   if word 1 of the name of the target is not "field" then 
      return "Invalid target"
   else if pValue is not a number then
      return "Invalid value"
   else
      put format ("£%1.2f",pValue) into the target
   end if
end cDisplayCurrency

setProp cDisplaySepCurrency pValue
   local tSepValue
   if word 1 of the name of the target is not "field" then 
      return "Invalid target"
   else if pValue is not a number then
      return "Invalid value"
   else
      put format ("%1.2f", pValue) into pValue
      put char -6 to -1 of pValue into tSepValue
      put empty into char -6 to -1 of pValue
      repeat while the number of chars in pValue > 0
         put char -3 to -1 of pValue & comma before tSepValue
         put empty into char -3 to -1 of pValue
      end repeat
      put "£" before tSepValue
      put tSepValue into the target
   end if
end cDisplaySepCurrency

setProp cDisplayPercentage pValue
   local tPctValue
   if word 1 of the name of the target is not "field" then 
      return "Invalid target"
   else if pValue is not a number then
      return "Invalid value"
   else
      put format ("%3d%%", pValue) into tPctValue
      --rounded to integer values.
      replace space with empty in tPctValue
      put tPctValue into the target
   end if
end cDisplayPercentage
To use, you can

Code: Select all

set the cDisplaySepCurrency of field "fldCurrencyValue" to 123456789
which will put into the field the value of: £123,456,789.00
The cDisplayPercentage setProp handler will round to an integer value of length 3, and then drop the leading space if it's not 100%, so you could

Code: Select all

put 73.7135 into tAverage
set the cDisplayPercentage of field "fldResults" to tAverage
and the field will have the contents: 74%

If you read up on the C "printf" function you should hopefully get a few clues as to the formatting options you can have.

HTH

andrew99
Posts: 11
Joined: Mon Mar 23, 2009 12:49 pm

Number formats

Post by andrew99 » Wed Mar 25, 2009 1:16 pm

Wow,

Many thanks SparkOut. I've not implemented your suggestions but had to say in advance thanks for your assistance thats more than i'd hoped for!

Will let you know how I get on.

Andrew

andrew99
Posts: 11
Joined: Mon Mar 23, 2009 12:49 pm

Calculating variables with set number formats

Post by andrew99 » Thu Mar 26, 2009 6:58 pm

SparkOut thanks for all your help on number formats, but I've got a related and probably simple question.

How do I use a field value set to a particular number format for a calculation?

ie. my code is as below and returns a compile error. I assume because revolution doesn't recognise £50,000.00 and 15.00% as values it can work with? Can you suggest a solution?

set the cDisplaySepCurrency of field "Purchase_Price_1" to 50000
set the cDisplayPercentage of field "Output_Purchase_Deposit" to 15
put field "Purchase_Price_1" into v_Purchase_Price
put field "Output_Purchase_Deposit" into v_Purchase_Deposit
put (v_Purchase_Price * v_Purchase_Deposit) into v_Deposit

Many thanks.

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

Post by SparkOut » Thu Mar 26, 2009 7:38 pm

Well, unfortunately "format" does make a string representation of the number, although Rev, being a decently helpful soul, will try to make the best sense of the values it is presented with. If it weren't for the percentage or currency symbol, or the commas, then even represented as strings Rev should be able to make the calculations.

So, you could strip the currency/percent symbols and commas before making the calculations.
Or, you could have a "duplicate" hidden field that you get the setProp handler to populate with the unadjusted numeric value specified.
Or, probably the best way, you could put the line:

Code: Select all

pass cDisplaySepCurrency
as the last line of the setProp handler (and similarly with the other setProp names of course). That will allow Rev to actually set the custom property specified (so becoming a legitimate custom property of the field instead of a virtual property). If you look in the Property Inspector for the relevant field, and see the custom property pane, currently there will be nothing shown. If you include the "pass" line in each relevant handler, then after a setProp handler has dealt with the task, you will see the property has been stored against the field in question.
Then when you need to retrieve the value for calculation, instead of

Code: Select all

set the cDisplaySepCurrency of field "Purchase_Price_1" to 50000 
set the cDisplayPercentage of field "Output_Purchase_Deposit" to 15 
put field "Purchase_Price_1" into v_Purchase_Price 
put field "Output_Purchase_Deposit" into v_Purchase_Deposit 
put (v_Purchase_Price * v_Purchase_Deposit) into v_Deposit
you would have

Code: Select all

set the cDisplaySepCurrency of field "Purchase_Price_1" to 50000 
set the cDisplayPercentage of field "Output_Purchase_Deposit" to 15 
put the cDisplaySepCurrency of field "Purchase_Price_1" into v_Purchase_Price 
put the cDisplayPercentage of field "Output_Purchase_Deposit" into v_Purchase_Deposit 
put (v_Purchase_Price * v_Purchase_Deposit) into v_Deposit 
You could always shorten that to:

Code: Select all

set the cDisplaySepCurrency of field "Purchase_Price_1" to 50000 
set the cDisplayPercentage of field "Output_Purchase_Deposit" to 15 
put the cDisplaySepCurrency of field "Purchase_Price_1" * the cDisplayPercentage of field "Output_Purchase_Deposit" into v_Deposit 
Because you can round or otherwise adjust the value before displaying it in the field, you might need to bear in mind that the "passed" custom property may not literally be the same as the displayed one. (Eg in the example I gave for the percentage, the 73.7135 that was rounded in the field to 74% would have the cDisplayPercentage property value of 73.7135 still.)
(You can round the value to the same as the display beforehand if necessary, but you would have to then specificially set the cDisplayPercentage of the target to the newly rounded value inside the setProp handler - so in order to avoid a runaway recursion, you would also need to lock messages before the value was set. Otherwise you would trigger another setProp to happen, which would do the same again, and trigger another, etc etc). HTH, and don't worry too much about the last bit unless you have to - I don't want to muddy the waters too much, as I would love to help clarify the mystery of custom and virtual properties, not make it more murky.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”