Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help please  RSS feed

 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:-


When I run this part of code, I always end up with the 'zero' message_string
I have tried checking the abs value of (myCurrPositionValue - 0.2) say being less the 0.001 but the same result occurs

What am I doing wrong?

Bob M
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not compare Strings with == operator.
Use equals() method instead.

AvoidTheEqualityOperator (this is a link).

Also, instead ofyou can (and should) write
Also, by convention, you should use camelCaseVariableNames instead of underscored_variable_names.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're testing curr_trade_dir for identity, not equality. When you write curr_trade_dir == "Down", you're testing whether they are exactly the same two objects in memory, not whether they mean the same thing. The lesson is simple: Don't use == where you should use equals().

Another thing, doubles can not represent certain values exactly. Doing comparisons like myCurrPositionValue == 0.2 is asking for trouble. If you really must use floating point arithmetic, you need to introduce a tolerance when you want to compare two values.

Finally, don't use variable names such as curr_trade_dir. Use currentTradeDirection. Strings are also a poor substitute for expressive data types. Use an enum for your directions:
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł is much quicker than I am today.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please UseAMeaningfulSubjectLine (this is also a link).
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

Bob M
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!