hi I am trying to find out if a 'var' is not equal to more than one number;
but the '!=' seems to only allow for one number. is there a work around?
thanks: )
if(lineNum != 100, 103)
{
$("#pic1-div").animate({
left: "+=150"
}, 1000 );
thanks to Klyarash for the answer below : )
i cant see where to thumbs up or vote 'answered' but this did the trick!!! : )
You can use logic operands like AND->(&&) either OR->(||)...
try this if it's important for you that the lineNum wasn't equal to both numbers:
if(lineNum != 100 && lineNum != 103)
{
$("#pic1-div").animate({
left: "+=150"
}, 1000 );
}
OR try this if you want one of two numbers:
if(lineNum != 100 || lineNum != 103)
{
$("#pic1-div").animate({
left: "+=150"
}, 1000 );
}
Related
In what way can I refactor nested if statements like this? A condition is met, however there are some exceptions to the condition. I recognize that this starts to approach the Arrow Anti-Pattern.
Specifically, are there any dart language features that can help me refactor or re-write this code to be more clear?
Let's use "leap years" as an example:
// on every year that is evenly divisible by 4
// except every year that is evenly divisible by 100
// unless the year is also evenly divisible by 400
bool isLeapYear(int year) {
assert(!year.isNegative);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
}
return false;
}
return true;
}
return false;
}
You are combining boolean checks with boolean returns.
That probably means you can do it all in one expression.
I'd do:
bool isLeapYear(int year) =>
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
(Now, if you want to optimize, then modulo, %, is more expensive than bitwise and, &, so you can also do:
bool isLeapYear(int year) =>
year & 3 == 0 && (year & 15 == 0 || year % 100 != 0);
which should be slightly more efficient. Doing & 15, aka % 16, is sufficient to detecting being a multiple of 400 if you know the number is a multiple of 100.)
For the general case, you can reverse your if branches and return early:
bool isLeapYear(int year) {
if (year % 4 != 0) return false; // Not multiple of 4.
// Year is a multiple of 4.
if (year % 100 != 0) return true; // Not multiple of 100.
// Year is a multiple 100.
return year % 400 == 0;
}
(Noticing that
if (year % 400 == 0) {
return true;
}
return false;
is the same as return year % 400 == 0;.)
How can I create function that convert large number into shorten number with character in Dart?
like
1000 => 1K
10000 => 10K
1000000 => 1M
10000000 => 10M
1000000000 => 1B
There is a built-in function in Dart that can be used and it's simple:
var f = NumberFormat.compact(locale: "en_IN");
print(f.format(12345));
to make it a method:
getShortForm(var number) {
var f = NumberFormat.compact(locale: "en_US");
return f.format(number);
}
for this to work import
import 'package:intl/intl.dart';
Refer to this doc for more https://pub.dev/documentation/intl/latest/intl/NumberFormat-class.html
If you are looking for a hard way:
getShortForm(int number) {
var shortForm = "";
if (number != null) {
if (number < 1000) {
shortForm = number.toString();
} else if (number >= 1000 && number < 1000000) {
shortForm = (number / 1000).toStringAsFixed(1) + "K";
} else if (number >= 1000000 && number < 1000000000) {
shortForm = (number / 1000000).toStringAsFixed(1) + "M";
} else if (number >= 1000000000 && number < 1000000000000) {
shortForm = (number / 1000000000).toStringAsFixed(1) + "B";
}
}
return shortForm;
}
String toString(int value) {
const units = <int, String>{
1000000000: 'B',
1000000: 'M',
1000: 'K',
};
return units.entries
.map((e) => '${value ~/ e.key}${e.value}')
.firstWhere((e) => !e.startsWith('0'), orElse: () => '$value');
}
A simpler approach, if you only need the suffix. It may not be compiling, but this is the idea.
String getSuffix (int t)
{
int i = -1;
for ( ; (t /= 1000) > 0 ; i++ );
return ['K','M','B'][i];
}
Edit
This is the mathematical way to do it, and it compiles. The point is you are searching for the amount of "groups of 3 decimal" places:
x 000 - 1
x 000 000 - 2
and so on. Which is log1000 number.
String getSuffix (int num)
{
int i = ( log(num) / log(1000) ).truncate();
return (num / pow(1000,i)).truncate().toString() + [' ','K','M','B'][i];
}
The Intl package does this as "compact" numbers, but it has a fixed format and it will also change with different locales, which might or might not be what you want.
Make a class and used its static method every where.
class NumberFormatter{
static String formatter(String currentBalance) {
try{
// suffix = {' ', 'k', 'M', 'B', 'T', 'P', 'E'};
double value = double.parse(currentBalance);
if(value < 1000){ // less than a thousand
return value.toStringAsFixed(2);
}else if(value >= 1000 && value < (1000*100*10)){ // less than a million
double result = value/1000;
return result.toStringAsFixed(2)+"k";
}else if(value >= 1000000 && value < (1000000*10*100)){ // less than 100 million
double result = value/1000000;
return result.toStringAsFixed(2)+"M";
}else if(value >= (1000000*10*100) && value < (1000000*10*100*100)){ // less than 100 billion
double result = value/(1000000*10*100);
return result.toStringAsFixed(2)+"B";
}else if(value >= (1000000*10*100*100) && value < (1000000*10*100*100*100)){ // less than 100 trillion
double result = value/(1000000*10*100*100);
return result.toStringAsFixed(2)+"T";
}
}catch(e){
print(e);
}
}
}
I am totally drain out as the second if statement couldn't be executed.
My original idea was when volatility is in a range of 90 - 110, the program will send one and only one order. And it will wait and see till the volatility reaches in a range of 111 - 150, and then it will send the second order.
If I don't use a bool function here, the program will send countless order when the range is reached.
Could someone please help me?
if ( TodayMaxVolatilityPercentage >= 90.0
&& ( dayTrend == 1 )
&& orderOpened == false
)
{
Print( "Entered if clause" );
// Print( "Today volatility percentage is: ", TodayMaxVolatilityPercentage + "%" );
// ticket: Returns number of the ticket assigned to the order by the trade server or -1 if it fails.
ticket = OrderSend( Symbol(),
OP_SELL,
0.3,
Bid,
3,
0 * MyPoint,
30 * MyPoint,
NULL,
MagicNumber,
0,
Blue
);
Print( "Order is opened on", OrderOpenTime()+" at price: ", OrderOpenPrice() );
Print( "trend number is ",dayTrend );
if ( ticket > 0 )
{
if ( TakeProfit > 0 ) TheTakeProfit = Bid - TakeProfit * MyPoint;
OrderSelect( ticket, SELECT_BY_TICKET ); // bool value
/* OrderModify( OrderTicket(),
OrderOpenPrice(),
0,
NormalizeDouble( TheTakeProfit, Digits ),
0,
Green
);
*/
}
orderOpened = true;
if ( TodayMaxVolatilityPercentage >= 110.0 ) orderOpened = false;
}
if ( TodayMaxVolatilityPercentage >= 110.0
&& ( dayTrend == 1 )
&& orderOpened == false
)
{
Print( "Entered second if clause" );
// ticket: Returns number of the ticket assigned to the order by the trade server or -1 if it fails.
ticket = OrderSend( Symbol(),
OP_SELL,
0.3,
Bid,
3,
0 * MyPoint,
30 * MyPoint,
NULL,
MagicNumber,
0,
Blue
);
if ( ticket > 0 )
{
if ( TakeProfit > 0 ) TheTakeProfit = Bid - TakeProfit * MyPoint;
OrderSelect( ticket, SELECT_BY_TICKET ); // bool value
/* OrderModify( OrderTicket(),
OrderOpenPrice(),
0,
NormalizeDouble( TheTakeProfit, Digits ),
0,
Green
);
*/
}
orderOpened = true;
}
A hidden show-stopper:
By design, the first successful OrderSend() returns a value you assign into an ticket = OrderSend(...).
The devil is hidden in detail.
[MQL4-Help] says:
Returns number of the ticket assigned to the order by the trade server or -1 if it fails.
So the first successful OrderSend() ticket# returned is 0 ( in StrategyTester, a real MT4/Server rather runs into high numbers drawn from it's db.pool of tickets ).
Thus the second if ( ticket > 0 ) will never let you in.
It is common to rather use if ( ticket > EMPTY ) so as to also clarify the intention and use symbolic instead of a -1 integer constant.
Say that I have the following map in dart:
Map f = {
0 : 0,
1 : 1,
2 : 0,
3 : 1,
4 : 0,
5 : 1
};
Is there something in dart so that you can easily work with the inverse map of the map f? So for example, the inverse map f⁻¹[0] (in math notation) should be equal to the set 0, 2, 4 in this case.
If values are unique then it's just:
Map inverse(Map f) {
return f.map( (k, v) => MapEntry(v, k) );
}
it doesn't complain about duplicates, it overwrites them.
Map f = {
0 : 0,
1 : 1,
2 : 0,
3 : 1,
4 : 0,
5 : 1
};
main() {
print(f.keys.where((k) => f[k] == 0));
// or
print(new Map.fromIterable(f.values.toSet(),
key: (k) => k,
value: (v) => f.keys.where((k) => f[k] == v)));
}
try at DartPad
There's also the BiMap class in quiver.dart library (although it might be overkill to use a library for just this single purpose).
Here is an example real-world usage of this class.
Inspired by gunter's answer:
Map inverse(Map f) {
Map inverse = {};
f.values.toSet().forEach((y) {
inverse[y] = f.keys.where((x) => f[x] == y).toSet();
});
return inverse;
}
DartPad demo
I want to display couple of points on a highcharts line chart which are big numbers.
e.g. 100,000, 10,000,000, 1,000,000,000
When I display these, the y axis automatically formats the number into 100 k, 10 M, 1,000 M etc but the tooltip still shows the actual big number.
Is it possible to show 1,000,000,000 as 1 B or 1000 M in the tooltip itself.
Example - http://jsfiddle.net/ynCKW/1/
I am trying to play with the numberFormat function but I dont think its the right function.
Highcharts.numberFormat(this.y,0)
Do I have to write a custom function which would do this formatting in the tooltip?
You can use the same logic as implemented in Highcharts core:
tooltip: {
formatter: function () {
var ret = '',
multi,
axis = this.series.yAxis,
numericSymbols = ['k', 'M', 'G', 'T', 'P', 'E'],
i = numericSymbols.length;
while (i-- && ret === '') {
multi = Math.pow(1000, i + 1);
if (axis.tickInterval >= multi && numericSymbols[i] !== null) {
ret = Highcharts.numberFormat(this.y / multi, -1) + numericSymbols[i];
}
}
return ret;
}
},
And jsFiddle: http://jsfiddle.net/ynCKW/2/
EDIT for Highcharts v6:
We can call build-in method, which should be easier to maintain: http://jsfiddle.net/BlackLabel/ynCKW/104/
tooltip: {
valueSuffix: '',
formatter: function () {
var axis = this.series.yAxis;
return axis.defaultLabelFormatter.call({
axis: axis,
value: this.y
});
}
},
Piggybacking off #Pawel Fus, a slight tweak allows you to have negative currency values as well but with the negative outside the $ (i.e. -$100K versus -$-100k).
function () {
var isNegative = this.value < 0 ? '-' : '';
var absValue = Math.abs(this.value);
return isNegative + '$' + this.axis.defaultLabelFormatter.call({
axis: this.axis,
value: absValue
});
}
Here is a jsFiddle: http://jsfiddle.net/4yuo9mww/1/