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(",", "."));
Related
i have a small problem. I have two Arrays where i try to find the same content in it. So i decided to convert it to a Set and then using those nice functions with "subtract". However i get very different results. Can someone tell me why this happens? When i use "subtracting" instead of "subtract" i get no problems however this is very weird for me and i really have no clue why this happens.
var objectIDsWhichExist = [ "kjugsJHL6JYoByOreUQ0wUefsbX2", "18ixZ21PJDXA1WzeJqZzctl7tTk2", "ZeQPYGfDvWMLSVykb4M5FQ6miGX2"]
var helperObjectIDsWhichExistInAdded = [ "kjugsJHL6JYoByOreUQ0wUefsbX2", "18ixZ21PJDXA1WzeJqZzctl7tTk2"]
var setA = Set(objectIDsWhichExist) /* Updated Data*/
var setB = Set(helperObjectIDsWhichExistInAdded) /* Standard Data*/
let different = setA.subtract(setB) // I GET HERE ()
print(different) // I GET THIS RESULT "()\n"
And this is the one sample which works, surprisingly. However i still dont know really why???
var employees: Set = Set(objectIDsWhichExist)
let neighbors: Set = Set(helperObjectIDsWhichExistInAdded)
employees.subtract(neighbors)
print(employees) // HERE IT DOES WORK because i get this -> ["ZeQPYGfDvWMLSVykb4M5FQ6miGX2"]\n"
subtract and subtracting both computes the difference of 2 sets, but subtract is mutating, whereas subtracting is not.
This means that x.subtract(y) changes the set x to the computed difference, whereas x.subtracting(y) doesn't change x at all, and instead returns the difference. On the other hand, subtract returns nothing (Void).
When you do
let different = setA.subtract(setB) // I GET HERE ()
print(different)
you see () being printed, because that is what the string representation of Void looks like - an empty tuple.
This works
employees.subtract(neighbors)
print(employees)
because subtract changes employee.
This also works:
let different = setA.subtracting(setB)
print(different)
because the return value of subtracting - the set difference - is assigned to different. Note that this doesn't change setA.
This doesn't work:
employees.subtracting(neighbors)
print(employees) // still shows the original employees
Because subtracting doesn't change employees, and you are ignoring its return value.
There are many other pairs of such mutating vs non-mutating methods, like
Set.formUnion vs Set.union
String.append vs String.appending
Related post
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
This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 11 months ago.
I'm fairly new to JavaScript and wanted to convert one of my calculation spreadsheets to something I can use on a web page. However, the calculation gives me an incorrect result.
Here's my JavaScript:
<script type="text/javascript" language="JavaScript">
function calc() {
var onerepmax = document.wodCalculate.oneRepMax.value;
var percent = document.wodCalculate.percentOfOneRepMax.value / 100;
var addkg = document.wodCalculate.plusKg.value;
var zwischenschritt = onerepmax * percent;
var gewicht = zwischenschritt + addkg;
document.getElementById("weight").innerHTML = gewicht;
};
</script>
and here's the HTML:
<form action="" id="wodCalculate" name="wodCalculate">
<table cellspacing="0" cellpadding="10" border="0">
<tr><td>1 Rep Max</td><td><input type=text name="oneRepMax" value=""> kg<br></td></tr>
<tr><td>% vom 1RM</td><td><input type=text name="percentOfOneRepMax" value=""> %<br></td></tr>
<tr><td>Plus x kg</td><td><input type=text name="plusKg" value=""> kg<br></td></tr>
<tr><td><input type="button" value="Calculate" onClick="calc()"></td></tr>
</table>
</form>
<div id="weight">Weight</div>
It works fine up to the point of multiplying the "onerepmax" with the "percent". However, once it gets to the point of adding the "addkg" to the result of the multiplication (i.e. giving me the "gewicht"), the result becomes incorrect.
For example, I want to get 10% of 100kg and add 10kg. Instead of 20kg, the calculation result is 1010.
Thanks for your help!
The value of an input is always a string, so + ends up being string concatenation ("10" + "10" is "1010", as opposed to 10 + 10 which is 20).
If you're using an input type="number" (the OP isn't, but others finding this answer may) and the browser supports them, you can use valueAsNumber instead:
var onerepmax = document.wodCalculate.oneRepMax.valueAsNumber;
If you're using type="text" or the browser doesn't support valueAsNumber:
You can convert user-input values using parseInt(value, 10) (the 10 = decimal, base 10) if they're meant to be whole numbers, e.g.:
var onerepmax = parseInt(document.wodCalculate.oneRepMax.value, 10);
That's just one option, though, you have several:
The unary + operator: value = +value will coerce the string to a number using the JavaScript engine's standard rules for that. The number can have a fractional portion (e.g., +"1.50" is 1.5). Any non-digits in the string (other than the e for scientific notation) make the result NaN. Also, +"" is 0, which may not be intuitive.
var onerepmax = +document.wodCalculate.oneRepMax.value;
The Number function: value = Number(value). Does the same thing as +.
var onerepmax = Number(document.wodCalculate.oneRepMax.value);
The parseInt function, usually with a radix (number base): value = parseInt(value, 10). The downside here is that parseInt converts any number it finds at the beginning of the string but ignores non-digits later in the string, so parseInt("100asdf", 10) is 100, not NaN. As the name implies, parseInt parses only a whole number.
// (Same as the first one above)
var onerepmax = parseInt(document.wodCalculate.oneRepMax.value, 10);
The parseFloat function: value = parseFloat(value). Allows fractional values, and always works in decimal (never octal or hex). Does the same thing parseInt does with garbage at the end of the string, parseFloat("123.34alksdjf") is 123.34.
var onerepmax = parseFloat(document.wodCalculate.oneRepMax.value);
So, pick your tool to suit your use case. :-)
The problem is that you're "adding" a string, not a number. The + operator in JavaScript has multiple semantics, and as soon as one operand is a string, it will do string concatenation.
To prevent that, convert the string to a number with the unary + operator.
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.
I have a cell array, c, filled with hexadecimal data and when I view the cell contents by typing c at the matlab prompt, it shows me contents enclosed between ticks, i.e., '0x0009'. But, one element is enclosed in brackets and looks like [650345]. How can I convert the [ ] data to ' ' data? When I do iscellstr on this particular element, matlab returns 0. iscellstr returns 1 for all other elements of c.
I'm reading this data into matlab from excel and I fear that excel 'helped' me by converting one hex value to scientific notation. I can't, as far as I've found, change what excel did. I think the true value is lost and unrecoverable. But I need to convert this one outstanding value, even if incorrect, to be like the other cell values so that I can carry on with my processing. Any suggestions?
If you know the index of wrong value and it's true value, you just do:
c(idx) = {'0x0009'};
I think this does what you want:
ind = cellfun(#isnumeric, c); %// find numeric cells
c(ind) = cellfun(#(s) ['0x' dec2hex(s)], c(ind), 'uniformout', 0); %// convert to
%// hex string and prepend '0x'
Example: input
c = {'0x0009', 650345};
produces the output
c =
'0x0009' '0x9EC69'