All is in the question, is there a way to display the € symbol in extjs?
I tried
var euroMoney = function(v) {
v = (Math.round((v - 0) * 100)) / 100;
v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.'),
whole = ps[0],
sub = ps[1] ? ',' + ps[1] : ',00',
r = /(\d+)(\d{3})/;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + '.' + '$2');
}
v = whole + sub;
return v + " €";
}
But for euroMoney(1) it returns me
1,00 €
This happens when your text editor saves it the wrong way. Make sure the encoding in your text editor is set to UTF-8 and you should be fine.
Related
I have this script:
strategy("My strategy")
var float start_price = na
var float end_price = na
var float[] start_prices = array.new_float(0)
var float[] end_prices = array.new_float(0)
var float p = na
f(x) => math.round(x / 500) * 500
lo = (high + close) / 2
var i = 0
if bar_index == 1
start_price := f(lo)
end_price := f(start_price * 1.015)
else
if close <= start_price
strategy.entry(str.format("Long {0}",i), strategy.long)
array.push(end_prices, end_price)
array.push(start_prices, end_price)
i := i + 1
start_price := start_price - 500
end_price := f(start_price * 1.015)
for j = 0 to (array.size(end_prices) == 0 ? na : array.size(end_prices) - 1)
p := array.get(end_prices, j)
if close >= p
strategy.exit(str.format("Long {0}",j), limit=end_price)
I want to console/debug/display the values in start_prices array
But I can't figure out for the life of me how to do that, there's no console.log or anything like that. I'm a somewhat competent python programmer, but I always use the print()... Anyway, how do people debug in this language?
You can use the tostring() function (str.tostring() in v5) to generate a string of your array. You can then output it into a label or table.
eg.
start_prices_string = str.tostring(start_prices)
debug = label.new(x = bar_index, y = close, style = label.style_label_left, text = start_prices_string)
label.delete(debug[1])
I am trying to define a function in scala ( ^ ), which takes 2 values and prints them like
2
x
Here is what I have so far...
class $ (val text2D: Array[Array[Char]])
{
def ^(that: $) =
{
" " ++ s"${this.text2D(0)(0)}" ++
"\n" ++ s"${that.text2D(0)(0)}"
}
def +(that: $) = this.text2D + "+" + that.text2D
override def toString = s"${this.text2D(0)(0)}"
}
object $ {
val array = Array.ofDim[Char](1,1)
def apply(x: String): $ = {
array (0)(0) = x.charAt(0)
new $ (array)
}
}
val x = $("x")
println(x)
val x2 = $("x") ^ $("2")
println(x2)
When I run this, I do not get the output I am expecting, instead I get
2
2
Why is it only taking the second element? Any help would be appreciated.
object creates a singleton, so the (mutable) array that you use is shared between calls to apply. You need to allocate that array inside the apply call.
def apply(x: String): $ = {
val array = Array.ofDim[Char](1,1)
array (0)(0) = x.charAt(0)
new $ (array)
}
Also, slightly unrelated, but I believe you have your arguments backward. To get the output you want, you need
" " ++ s"${that.text2D(0)(0)}" ++
"\n" ++ s"${this.text2D(0)(0)}"
I think what you need is something like this:
class $(val text2D: Array[String]) {
def ^(that: $): $ = {
if (this.text2D.length == 0)
that
else if (that.text2D.length == 0)
this
else {
val thisW = this.text2D(0).length
val thatW = that.text2D(0).length
// cross-pad arrays to have the same width
val padThisRight = " " * thatW
val padThatLeft = " " * thisW
val thisPaddedW = this.text2D.map(_ + padThisRight)
val thatPaddedW = that.text2D.map(padThatLeft + _)
// first lines comes from that!
new $(thatPaddedW ++ thisPaddedW)
}
}
def +(that: $): $ = {
if (this.text2D.length == 0)
that
else if (that.text2D.length == 0)
this
else {
val thisH = this.text2D.length
val thatH = that.text2D.length
val thisW = this.text2D(0).length
val thatW = that.text2D(0).length
// pad arrays to have the same height
val emptyThis = " " * thisW
val emptyThat = " " * thatW
val thisPaddedH = if (thisH >= thatH) this.text2D else Array.fill(thatH - thisH)(emptyThis) ++ this.text2D
val thatPaddedH = if (thisH <= thatH) that.text2D else Array.fill(thisH - thatH)(emptyThat) ++ that.text2D
new $(thisPaddedH.zip(thatPaddedH).map(p => p._1 + p._2))
}
}
override def toString = text2D.mkString("\n")
}
object $ {
def apply(x: String): $ = {
new $(Array[String](x))
}
}
and then
val x2 = $("x") ^ $("2")
println(s"x2:\n$x2")
println("----------------------------")
val z = x2 + $(" + ") + y2
println(s"z:\n$z")
println("----------------------------")
val zz = x2 + $(" + ") + (y2 ^ $("3"))
println(s"zz:\n$zz")
println("----------------------------")
produces following output
x2:
2
x
----------------------------
z:
2 2
x + y
----------------------------
zz:
3
2 2
x + y
----------------------------
The main idea here is that operations on $ produce another instance of $ rather than String (I use String instead of Array[Char] as it seems much easier and has no obvious drawbacks). In such way you don't have to re-parse String splitting it by new lines and have to wonder how to handle cases when the string is not well-aligned. So now operators ^ and + is just an exercise in aligning two 2d-arrays to have either the same width or the same height and then joining them.
Good evening everyone!
Anyone knows how to extract the top 5 numbers (the highest) from an array?
I have an XML with names, surnames and score and want to extract the top 5 to create a chart.
As for now, I am getting all the info in this way:
num = xmlData.childNodes.length;
for (var i = 0; i <= num - 1; i++)
{
names[i] = this.childNodes[i].childNodes[6].firstChild.nodeValue;
surnames[i] = this.childNodes[i].childNodes[5].firstChild.nodeValue;
points[i] = this.childNodes[i].childNodes[0].firstChild.nodeValue;
pointsint[i] = parseInt(speeds[i]);
trace(i + "." + pointsint[i] + " (" + names[i] + " " + surnames[i] + ")");
}
Thank you for any help!
Organize data as generic object entries and Array.sortOn(...) (http://help.adobe.com/en_US/as2/reference/flashlite/WS5b3ccc516d4fbf351e63e3d118ccf9c47f-7ea5.html). I didn't test it but I think the idea is clear:
var num = xmlData.childNodes.length;
var entries = [];
for (var i = 0; i <= num - 1; i++)
{
var anEntry = {};
var aNode = xmlData.childNodes[i];
anEntry['name'] = aNode.childNodes[6].firstChild.nodeValue;
anEntry['surname'] = aNode.childNodes[5].firstChild.nodeValue;
var aSpeed = aNode.childNodes[0].firstChild.nodeValue;
anEntry['speed'] = parseInt(aSpeed);
entries.push(anEntry);
trace(i + "." + anEntry['speed'] + " (" + anEntry['name'] + " " + anEntry['surname'] + ")");
}
entries.sortOn("speed", Array.DESCENDING | Array.NUMERIC);
// Now first five elements of entries Array is what you want.
I have following anonymous function (with x as an array):
f = #(x) 312*x(2) - 240*x(1) + 30*x(3) - 24*x(4) + 282*x(1)*x(2) + 30*x(1)*x(3) + 18*x(1)*x(4) + 54*x(2)*x(3) + 6*x(2)*x(4) + 6*x(3)*x(4) + 638*x(1)^2 + 207*x(2)^2 + 6*x(3)^2 + 3*x(4)^2 + 4063
I want to make gradient of this function and save it for future use. Also with array input.
X = [ 0;...
0;...
0;...
0];
F = f(X)
G = g(X)
Is it possible to archive this with this type of function? Or maybe it is possible to somehow make it via diff command? Something like this:
g = [diff(f, x(1));...
diff(f, x(2));...
diff(f, x(3));...
diff(f, x(4))]
I guess the following is what you want. I'm afraid, you need the Symbolic Math Toolbox for a simple solution, otherwise I'd rather calculate the derivatives by hand.
x = [1 2 3 4];
%// define function
syms a b c d
f = 312*b - 240*a + 30*c - 24*d + 282*a*b + 30*a*c + 18*a*d + 54*b*c + ...
6*b*d + 6*c*d + 638*a^2 + 207*b^2 + 6*c^2 + 3*d^2 + 4063
%// symbolic gradient
g = gradient(f,[a,b,c,d])
%// eval symbolic function
F = subs(f,[a,b,c,d],x)
G = subs(g,[a,b,c,d],x)
%// convert symbolic value to double
Fd = double(F)
Gd = double(G)
or alternatively:
%// convert symbolic function to anonymous function
fd = matlabFunction(f)
gd = matlabFunction(g)
%// eval anonymous function
x = num2cell(x)
Fd = fd(x{:})
Gd = gd(x{:})
f =
638*a^2 + 282*a*b + 30*a*c + 18*a*d - 240*a + 207*b^2 + 54*b*c +
6*b*d + 312*b + 6*c^2 + 6*c*d + 30*c + 3*d^2 - 24*d + 4063
g =
1276*a + 282*b + 30*c + 18*d - 240
282*a + 414*b + 54*c + 6*d + 312
30*a + 54*b + 12*c + 6*d + 30
18*a + 6*b + 6*c + 6*d - 24
F =
7179
G =
1762
1608
228
48
fd =
#(a,b,c,d)a.*-2.4e2+b.*3.12e2+c.*3.0e1-d.*2.4e1+a.*b.*2.82e2+a.*c.*3.0e1+a.*d.*1.8e1+b.*c.*5.4e1+b.*d.*6.0+c.*d.*6.0+a.^2.*6.38e2+b.^2.*2.07e2+c.^2.*6.0+d.^2.*3.0+4.063e3
gd =
#(a,b,c,d)[a.*1.276e3+b.*2.82e2+c.*3.0e1+d.*1.8e1-2.4e2;a.*2.82e2+b.*4.14e2+c.*5.4e1+d.*6.0+3.12e2;a.*3.0e1+b.*5.4e1+c.*1.2e1+d.*6.0+3.0e1;a.*1.8e1+b.*6.0+c.*6.0+d.*6.0-2.4e1]
x =
[1] [2] [3] [4]
Fd =
7179
Gd =
1762
1608
228
48
In the code below, I am trying to read in a 2d array (157 rows, 2 columns), which are all formulas. I need to then delete a part of the formula from each cell, then write it back to the sheet in the same range (prior to this step, I am deleting the sheet that this portion of the formula is referencing). When I run the debugger, everything runs fine, but my values do not get replaced. Any thoughts?
ss.setActiveSheet(ss.getSheetByName('Employee List'));
var as = SpreadsheetApp.getActiveSheet();
var empid = as.getLastRow();
var hrsRange = as.getRange(1, 3, empid, 2).getFormulas();
var i = 1;
while(i < empid)
{
var emp = as.getRange(i, 2).getA1Notation();
if (cpr == 20)
{
while(i < hrsRange.length)
{
var regHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!P:P)";
var otHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!Q:Q)";
hrsRange[i-1][0].toString().replace(regHrs,"");
hrsRange[i-1][1].toString().replace(otHrs,"");
i++;
}
} else {
while(i < hrsRange.length)
{
var regHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!Q:Q)";
var otHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!R:R)";
hrsRange[i-1][0].toString().replace(regHrs,"");
hrsRange[i-1][1].toString().replace(otHrs,"");
i++;
}
}
i++;
}
as.getRange(1, 3, empid, 2).setFormulas(hrsRange);
This line:
hrsRange[i-1][0].toString().replace(regHrs,"");
will return a string, not actually modify hrsRange[i-1][0] .
You'll need to assign the resulting string to hrsRange[i-1][0]:
hrsRange[i-1][0]=hrsRange[i-1][0].toString().replace(regHrs,"");