PDA

View Full Version : How to Manipulate Strings to be used for computation


ABC123

doorscomputers
06-29-2013, 09:18 AM
This is the computation that I would like to do on a computation to be saved on a field

Given
Price = 200
Discount = ?
There are 3 discount variations that I want to compute depending on the input of the user.
1.First variation of the Discount is when the user enters +6 as the discount
if the user entered +6 then the formula is = 200 + (200* (6/100))
to simplify that = 200 + (200*.06)
Result = 200 +12
Result = 212 Final Price

2. Second Variation of the Formula is when a user enter -6 or -7 or -9 etc. lets take - 6 again as our example
to simplify that = 200 + (200*-.06)
Result = 200 -12
Result = 188 Final Price
The first two variations are easy to without the third variation

3. The last variation is when the user enters 10+2, or 12+3, or 6+4 etc. lets take for example 10+2
to simplify that = (200 * 10) / (10+2)
(200*10)/12
Result = 166.67

another example of the 3rd variation is , lets say a user enters 12+3

(200 * 12) / (12+3)
(200*12) /15
Result = 160

How can I possibly compute for this is , my problem here is to determine for the user input of the discount if it is variation 1 or variation 2 or variation 3. Can how do I do this?

Tim Kiebert
06-29-2013, 10:26 PM
Try this.
If you are allowing the discount to be entered in a freeform text field then there is a lot of scope for errors. The code below attempts to catch some of these errors but is not complete. More could be done at the UI level.




dim price_lnv as N
dim disc_lcv as C
dim result_lnv as N
dim pluspos_lnv as N
dim minuspos_lnv as N


price_lnv = 200 'ui_get_number("Price","Enter a price:") '\\enter starting price.

disc_lcv = ui_get_text("Discount","Enter a discount:") '\\enter the discount
pluspos_lnv = at("+",disc_lcv) '\\determine the position, if any, of
minuspos_lnv = at("-",disc_lcv) '\\ the + or - symbol (sign)


'\\check for improper discount string
'\\ --- missing symbol -------- minus used in variation 3 ----- both symbols used -----------
If (pluspos_lnv + minuspos_lnv < 1) .or. minuspos_lnv > 1 .or. (pluspos_lnv > 0 .and. minuspos_lnv > 0) then
msgbox("Incorrect discount specified" +crlf(2) + "\""+disc_lcv + "\"")
end
end if
'\\there are still more possible errors in the discount input string. Either add more tests here or change the
'\\user interface to eliminate some of the possible errors. ie "10 2+"
select
case pluspos_lnv = 1 '\\variation 1
result_lnv = price_lnv + (price_lnv * val(word(disc_lcv,2,"+"))/100)
case minuspos_lnv = 1 '\\variation 2
result_lnv = price_lnv - (price_lnv * val(word(disc_lcv,2,"-"))/100)
case pluspos_lnv > 1 '\\variation 3
result_lnv = (price_lnv * val(word(disc_lcv,1,"+"))) / eval(disc_lcv)
end select

result_lnv = round(result_lnv,2)
msgbox("",""+result_lnv)

doorscomputers
06-30-2013, 08:43 AM
Thank you so much Tim, the key command that you showed me that solves what I want to achieve are these two commands at("+",disc_lcv) and at("-",disc_lcv). My problem now is, how to get the value of 200 which is the user given price and the discount when I am on a dialog component or a grid component by not using the prompt ui commands. eg. In visual basic .net or C# for example, when I want to get or set the value of a textbox called txtPrice and txtDiscount then I would write txtPrice.Text and txtDiscount.Text. How do I do this on text on a dialog component? and how do I get the text value when I am on a grid component on any column depending on the location or column of the price?

Tim Kiebert
06-30-2013, 09:36 AM
How do I do this on text on a dialog component? and how do I get the text value when I am on a grid component on any column depending on the location or column of the price?Uhmmm, i think this is a question for a web forum.

doorscomputers
06-30-2013, 11:40 AM
Uhmmm, i think this is a question for a web forum.

Can be on the desktop right? web components for the desktop or WCD as they term it here. not sure but I have some grid and dialog components on my desktop app.

doorscomputers
07-03-2013, 02:41 AM
I want to apply the Code that Tim posted and I think it is the right way to do it, I want to do it in a dialog component but I dont know how to reference the input fields in code when I am in a dialog component, also I tried to find the OnDepart event on the discount field so that after specifying the discount , when a user leaves the field then it will automatically display the computed result in the result field. I have attached an image of a dialog component and some sample data entry fields. Please help!

Ray in Capetown
07-03-2013, 05:36 AM
Roman




1.First variation of the Discount is when the user enters +6 ....Result = 200 +12 = 212 Final Price
2. Second Variation of the Formula is when a user enter -6 ....Result = 200 -12 = 188 Final Price
3. The last variation is when the user enters 10+2 ...........(200*10)/12 = 166.67
You really need to be consistent
case1 plus 6 discount increases the price by the perc
case 2 minus 6 reduces the price by the perc
case 3 PLUS 10+2 REDUCES THE PRICE opposing case1

Maybe different over here but in price negotiations at wholesale level common speak is, for example
eg.1 "less 10" - is understood as 200.00 less 10% (20.00) = 80
eg,2."less 10+5" is understood as 200.00 less 10% 20.00) = 80, less another 5% (16.00) = 64.00
is that what you are attempting?

doorscomputers
07-03-2013, 02:08 PM
Roman


You really need to be consistent
case1 plus 6 discount increases the price by the perc
case 2 minus 6 reduces the price by the perc
case 3 PLUS 10+2 REDUCES THE PRICE opposing case1

Maybe different over here but in price negotiations at wholesale level common speak is, for example
eg.1 "less 10" - is understood as 200.00 less 10% (20.00) = 80
eg,2."less 10+5" is understood as 200.00 less 10% 20.00) = 80, less another 5% (16.00) = 64.00
is that what you are attempting?

Thank you Ray for trying to help
Case 1 and Case 2 are standard discount that can be found on books and we have learned it in school.Option 3 is a custom formula, so do not concentrate on the formulas. The solution of Tim is the one that I am looking for, he tried to get for the value given by the user, then he tries to get the value if it starts with (+) positive then he executes formula 1, if he gets )-) negative then he executes Formula 2, and if the input does not start with negative or positive then he executes formula 3. The formulas are not the focus here. The solution is already given by Tim, but instead of prompting the user to input values on message prompt, I want the user to input the values on input fields in a grid component or dialog component for the desktop. but I dont know how to get the value on fields in an input field on the grid. IN visual basic, if I want to get the value of txtdiscount I type this, txtdiscount.text and if I want to assign a value on it, i would say txtDiscount.text = Price * Discount . On the desktop, it is very easy using action script right, point and click to get or set the values of fields and get or set the value of variables etc. How do I do this on Web Components? Thanks very much!

Ray in Capetown
07-03-2013, 04:28 PM
Aha. Ok

doorscomputers
07-05-2013, 07:53 AM
Here is the Solution in Javascript for newbies like me. The one given by Tim on Post #2 is the solution on the regular Alpha Five Form.
Put whatever function in the Grid component|Properties|Javascript Functions Declarations property . this video by Selwyn is useful for this here is the link http://www.ajaxvideotutorials.com/V10Videos/UsingUserDefinedJavascriptInClientSideCalculations.swf. Thanks!

function CompueDisc(Price,Dscount,sgn) {
var strPlus=Dscount;
var strMinus=Dscount;
var nPlus=strPlus.indexOf("+");
var nMinus=strMinus.indexOf("-");
var str=Dscount;
var n=str.split("+",2);

var nnn=strPlus.substr(0,2)
var nnnn=strMinus.substr(2)

if (sgn == "+")
{
//Put whatever calculations here
return Price + Dscount
}
else if
(sgn == "-")
{
//Put whatever calculations here
return Price * Dscount
}
else if (sgn == "c")
{
if (nPlus ==2)
{
//Put whatever calculations here
return Price + Dscount
}
else if (nMinus ==2)
{
//Put whatever calculations here
return Price + Dscount
}
else
{
//Put whatever calculations here
return Price + Dscount

}
}
}