4

Ok, I'm not a coder, I've no school about it but ... I can't figure out why this little operation returns a NaN value!

I've this var at the beginning

// Varialbes for simulation purpose
var Simulation = false;
var FakeCapital = 0;
var PercentOfTotal = 100;

// Variables
var Capital = (engine.getBalance() / 100).toFixed(2);
var UsableBalance = Math.floor(PercentOfTotal / 100 * Capital);
var StartingBalance = UsableBalance;

if (FakeCapital > 0 && Simulation) {
  Capital = FakeCapital;
  UsableBalance = Math.floor(PercentOfTotal / 100 * Capital);
  StartingBalance = UsableBalance;
}

So If I activate the similation var and if I want to use another capital, the script use the fakecapital to test my script.

Here all works but I think that here there's the problem, specially the UsableBalance = Math.floor(PercentOfTotal / 100 * Capital);

Because when the script run: If I don't use the simulation, all goes right If I use the simulation and the fake capital, all goes right

But if I use the simulation and I want to use the real capital, the UsableBalance var is strange, not immediately but when the script runs! I try to explain better

Let's assume that I use the simulation phase and I want to use the real capital

Your ballance is: 87.26 bits. I'll use: 87 bits for this session, as your request.

Here all ok, but this code:

if (TemporaryLoss <= 0) {
    Capital += LastProfit;
    UsableBalance = Math.floor((PercentOfTotal / 100) * Capital);
    TemporaryLoss = 0;
}

Return this:

TemporaryLoss: 0
Capital: 87.26
LastProfit: 1.0299999999999998
PercentOfTotal: 100
Capital: 87.261.0299999999999998

Why the math return this strange number? like a concatenation of 2 numbers? Seems that the script use the var like a text and not like a numbers.

Any Idea?

Jorman Franzini
  • 329
  • 1
  • 3
  • 17
  • you may need to use `parseInt` or `parseFloat` – Murad Hasan Apr 03 '16 at 07:39
  • It is worth taking a step back and think about how you handle currency values and loss of precision due to floating point conversions (particularly if you are using `Math.floor()` as you are here. See [the discussion here](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) for more information. Also in general, it is also worth handling currency values in `cents` (or whatever the lowest possible currency value is) or as strings to sidestep most basic precision problems. – Andrew Apr 03 '16 at 07:49
  • Possible duplicate of [Javascript - NaN](http://stackoverflow.com/questions/25747520/javascript-nan) – rockerest Apr 03 '16 at 07:56
  • In addition to the above (automated) comment: http://stackoverflow.com/search?q=javascript+nan almost every one of these questions and answers contains the correct answer. – rockerest Apr 03 '16 at 07:57

2 Answers2

3

You make a string with toFixed()

var Capital = (engine.getBalance() / 100).toFixed(2);

and used it later as number, but it is a string.

 Capital += LastProfit;

Solution: If fixed is necessary, then use parseFloat() to make a number again.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    No, that's not a problem, `*` converts the string back to number. – georg Apr 03 '16 at 07:42
  • @georg, right, but the problem is here: `Capital += LastProfit;` `Capital` is a string and adding keeps the string, with the above mentioned result of concatination. – Nina Scholz Apr 03 '16 at 07:51
  • Yes, that's correct. I'd suggest the OP just removes `toFixed` – georg Apr 03 '16 at 07:52
  • I try but why, if I use the script on normal purpose, read without fakecapital an without simulation, all works fine? I have error only on 1 of 3 possible use. Fixed is not necessary but, is better to read 20.23 instead 20.2299999999 – Jorman Franzini Apr 03 '16 at 08:59
  • use `toFixed` only at the end of calcualtion for presentation of the number. please have a look [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Nina Scholz Apr 03 '16 at 09:04
  • 1
    Ok tnx for the info! That works but, why in 2 of 3 case this works? Is always the same functions! But that works, tnx! – Jorman Franzini Apr 04 '16 at 19:43
0

Lets take an example :

var x = 1;

var y = '1';

var z = x + (y*10)

the datatype of variable y is implicitly coerced to number for the current operation, to evaluate the value of z.

So, for z , y is taken as a number because (y*10) caused implicit coercion, but doesn't change y itself to number

In your case ,

var Capital = (engine.getBalance() / 100).toFixed(2);

causes Capital to become a string. Hence any further addition operations with Capital result in concatenation You will have to explicitly convert to int or float as mentioned earlier.

HS Krishna
  • 128
  • 7