var selPage2 = $scope.selPage + $scope.itemsPerPage;
$scope.selPageUp = selPage2;
for example if value of selPage is 50 and value of itemsPerPage is 10 and I want to display my value in HTML element like
{{selPageUp}}
Angular JS will make connection of this two strings and will display it like 5010, but I want to sum up two values to display number 60. How to do that? Example if I change operator from + to * it will multiply value and display 500 it will work on way I want. Help please?
If they are integers:
var selPage2 = parseInt($scope.selPage) + parseInt($scope.itemsPerPage);
Otherwise:
var selPage2 = parseFloat($scope.selPage) + parseFloat($scope.itemsPerPage);
The $scope.selpage and $scope.itemPerPage are probably of type String. if they would be Integers it would sum up fine. so first you have to convert this string to an Integer of Float with parseInt() or parseFloat(). 2 Strings do multiply because * is a Math function so it will automatically TRY to convert these strings in values.
Related
I am working with arrays this time and I want to add elements. Whenever a user adds some value in the array I want it to be added to the previous value and give me the total value.
struct KeyValues{
var category:String!
var amount:String!
}
var arrExpense = [KeyValues]()
In my case, I am adding values to the amount variable. I want the sum of all the values user has added to the amount values. How can I achieve that in swift. Do I need to use loop or something else?
First of all never declare a struct member as implicit unwrapped optional. If it's supposed to be optional declare it as regular optional (?)
Second of all if a value won't change (maybe category) declare it as constant (let)
You can sum up the values with compactMap and reduce
struct KeyValues {
let category : String
var amount : String
}
let sum = arrExpense.compactMap{Int($0.amount)}.reduce(0, +)
compactMap is necessary to filter the strings which cannot be converted to Int. Consider to use a numeric type for amount
see this switf arrays article
let numbers = [1,2,3,5]
numbers.append(6) // [1,2,3,5,6]
numbers.insert(4, at: 3) // [1,2,3,4,5,6]
numbers.remove(at: 1) // [1,3,4,5,6]
numbers.removeLast() // [1,3,4,5]
let total = numbers.reduce(0, +) // 0 = starting point, + = operation
// 0 + 1 + 3 + 4 + 5 = 13
I want to use a random number from a list to select the particular element of a parameter array and use it elsewhere in the script as a parameter e.g.
Array is sspaidlist
My random integer from a parameter list is {GenRandomSSPAID}, which i want to use as the element of the sspaidlist array and save to is RandomSSPAID
lr_save_string(lr_eval_string("sspaidlist_{GenRandomSSPAID}"),"RandomSSPAID");
This just gets me the actual value "sspaidlist" and not the array.
I've also tried
sprintf(RandomSSPAID, "{sspaidlist_%d}", lr_eval_string("{GenRandomSSPAID}"));
but this seems to set RandomSSPAID to 0
The idea is to get 3 unique values - so 3 different array elements, I can't get the same value twice. I've offloaded the randomness to the loadrunner parameter functions, so I will always get a unique number with {GenRandomSSPAID}.
First convert your "GenRandomSSPAID" to integer as below:
i = atoi(lr_eval_string("{GenRandomSSPAID}"));
Now use sprintf to save it into RandomSSPAID as below:
sprintf(RandomSSPAID, "{sspaidlist_%d}", i);
You should be able to see value now.
I resolved this with the following code:
//declare c variables
Add_List()
{
....
char *RandomSSPAID;
char *SecondRandomSSPAID;
char *ThirdRandomSSPAID;
RandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(RandomSSPAID),"RandomSSPAID");
SecondRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(SecondRandomSSPAID),"SecondRandomSSPAID");
ThirdRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(ThirdRandomSSPAID),"ThirdRandomSSPAID");
lr_error_message("Random Values for iteration %s are : %s_%s_%s",lr_eval_string("{IterationNumber}"),lr_eval_string("{RandomSSPAID}"),lr_eval_string("{SecondRandomSSPAID}"),lr_eval_string("{ThirdRandomSSPAID}"));
....
}
Note that I offloaded the randomness to Loadrunner to generate a random number with {GenRandomSSPAID}, which is a parameter type of File, with a list of numbers and setting to select new row 'Random', Update value on 'Each occurance'
Working with JS. I have an array of objects. Here is a sample below. You can see that all the values are strings. However, I need to convert the lft and rgt values into integers so that I can compare these values against other values held in a different array. Keeping them as strings and comparing gives me erroneous results.
nodesWithchildren
:Array[1]
0:Object
category_id:"8"
created_at:"2016-11-04 14:14:28"
lft:"17"
name:"Felt Materials"
rgt:"22"
updated_at:"-0001-11-30 0:00:00"
__proto__:Object
I have searched through Stackoverflow and found various suggestions. All of them use for loops with parseInt which is great BUT they assume that the values either are all string numbers AND they assume that the values are held in a simple array. Not an array of objects.
I tried using this, but this does NOT work. Probably because parseint does not like it. IN fact it crashes the page:
var nodesWithchildrenParsed =[];
for(var i=0; nodesWithchildren.length;i++){
nodesWithchildrenParsed[i] = parseInt(nodesWithchildren[i]);}
My first step is converting these values. Once I have the lft and rgt converted to integers I will run another function where I will say something like, "if lft is greater than X and rgt is less than Y then give me the name "felt materials".
Tks !
The easy way to convert a string to a number without using parseint() is to simply divide the string by the integer 1. In my case this was a perfect solution as I did not have to deal with the fact that the strings were in an array of objects, other that doing this:
var childNodes=[];
for(i=0; i < selectedArray.length; i++){
if((selectedArray[i].lft /1 ) > (nodesWithchildren[0].lft /1) && (selectedArray[i].rgt/ 1) < (nodesWithchildren[0].rgt/1)) {
childNodes += selectedArray[i].name;
}
}
JS will convert the string before undertaking any operations on it. Cody's Example:
console.log( ('2'/1) > ('10'/1) );
Credit goes to Cody and his answer here:
Cody's answer
My array contains a string in the first row
how can I sum the array from the 2nd row to the Nth/1442th row (as in my example) disregarding the negative signs present in the column?
for example, my code for an array called data2 is:
S = sum(data2(2,15):data2(1442,15));
so sum all of the elements from row 2 to row 1442 in column 15.
This doesn't work but it also does not have anything to deal with the absolute value of whatever row its checking
data is from a .csv:
You should do something like this:
sum(abs(data(2:1442,15)));
The abs function will find the absolute value of each value in the array (i.e. disregard the negative sign). data(2:1442,15) will grab rows 2-1442 of the 15th column, as you wanted.
EDIT: apparently data is a cell array, so you could do the following, I think:
sum(abs([data{2:1442,15}]));
Ok so it looks like you have a constant column so
data2(2,15) = -0.02
and further down
data2(1442,15) = -0.02 %(I would assume)
So when you form:
data2(2,15):data2(1442,15)
this is essential like trying to create an array but of a single value since:
-0.02:-0.02
ans =
-0.0200
which of course gives:
>> sum(-0.02:-0.02)
ans =
-0.0200
What you want should be more like:
sum(data2(2:1442,15))
That way, the index: 2:1442, forms a vector of all the row references for you.
To disregard the negative values:
your answer = sum(abs(data2(2:1442,15)))
EDIT: For a cell array this works:
sum(abs(cell2mat(data2(2:1442,15))))
I used decimalSeparator property of numberfield and set it to ',' (comma). Now I have problem reading value as number and doing math with it.
I first tried simple:
var v = form.getForm().getValues().myfield / 2
As soon s I type comma, v becomes NaN.
Then I tried:
var v = Ext.util.Format.number(form.getForm().getValues().myfield, "0.000,00") / 2
As soon a I type comma, v becomes 0.
I also tried combinations with /i in format string but id didn't help either.
What am I missing?
The problem is that getValues does not work as you expect. From the documentation of getValues (emphasis mine):
Note: The values are collected from all enabled HTML input elements within the form, not from the Ext Field objects. This means that all returned values are Strings (or Arrays of Strings) and that the value can potentially be the emptyText of a field.
If you want to ensure that you have a decimal value, you will have to do something like the following:
var v_string = form.getForm().getValues().myfield; //get the string
v_string = v_string.replace(",", "."); //replace ',' with '.'
var v = (+v_string) / 2; //convert the value to a decimal and divide by 2
EDIT
You could also try using the getFieldValues (i.e. form.getForm().getFieldValues()) method instead of the getValues method. This should act as though you had called getValues on every form field.
This should work.
var value = parseFloat("5000,5".replace(",", "."));