Number formats
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Number formats
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.
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.
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.
To use, you can 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 couldand 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
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
Code: Select all
set the cDisplaySepCurrency of field "fldCurrencyValue" to 123456789
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
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
Number formats
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
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
Calculating variables with set number formats
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.
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.
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: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 ofyou would have
You could always shorten that to: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.
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
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
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
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
(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.