0

Here is my code first of all,

$(function() {
    var fee_percent = parseFloat('<?php echo $gateway["gateway_fees"]; ?>');
    var fee_fixed = parseFloat('<?php echo $gateway["gateway_fixed_fees"]; ?>');
    var min = parseFloat('<?php echo $gateway["min_invest_limit"]; ?>');
    var max = parseFloat('<?php echo $gateway["max_invest_limit"]; ?>');


    $("#amountId").on('change keyup keydown', function() {
        var $this = $(this);
        var val = parseFloat($this.val());
        val = val.toFixed(5);

        var valid = true;

        var fee = parseFloat(val * fee_percent / 100) + fee_fixed;
        fee = fee.toFixed(5);

        var final_amount = val - fee;
        final_amount = final_amount.toFixed(5);

        console.log("value: " + val);
        console.log("fee: " + fee);
        console.log("final: " + final_amount);
        console.log("min: " + min.toFixed(5));
        console.log("max: " + max.toFixed(5));

        if(val <= 0 || isNaN(val) || fee > val || val < min.toFixed(5) || val > max.toFixed(5)) fee = final_amount = 0;

        $("#depositFee").html(fee);
        $("#depositAmountFinal").html(final_amount);
    });
});

and now my problem is, val > max.toFixed(5) this is always returning TRUE.

Here is the console response :

value: 11.00000 fee: 0.46000 final: 10.54000 min: 10.00000 max: 10000.00000

so the value is 11 and max value in 10000, then why its false ?

If its not clear, please ask, thanks.

UPDATE : when I type 10, 100, or 1000 and so on, it works fine.

If I don't use max.toFixed(5) but instead I use only max, everything works fine but when I type some value with 5 in the end, like 55, 65, 75 and so on, the condition become true again, that should return false.

6
  • 11 is less than 10000, so it should be returning false. Sure you don't want to be using the < operator? Commented Mar 5, 2015 at 5:54
  • 11 is always less than 10000. Then it should return false only. Commented Mar 5, 2015 at 5:55
  • sorry that was my mistake, actually the condition returns true on that, but actually that is not correct so I said false. Commented Mar 5, 2015 at 5:58
  • @mohamedrias: "11" > "10000" :-) Commented Mar 5, 2015 at 6:08
  • @mohamedrias OK I take your example, now tell me, this condition will work or no ? fee = final_amount = 0;, this is from that if condition block, so if value is 11 and max is 10000, fee = final_amount = 0; should run or no ? According to me, NO, but actually it runs, that's my problem. Commented Mar 5, 2015 at 6:13

2 Answers 2

2

toFixed returns you String.

So "11" > "10000" returns you true because. 1 followed by 1 is greater than 1 followed by 0

You can try in your console:

"01" > "0001" will return you true which is also based on the same concept.

To avoid it :

parseFloat(val) > parseFloat(max.toFixed(5))
Sign up to request clarification or add additional context in comments.

6 Comments

This seems true, but whats the solution ?
Updated answer. parseFloat and check
It's because the interpreter is trying to type cast both operands to same type. when it was just 5 it converted "11.000" to number. But when it became 55 it converted it to String. And note that val in your case is having .toFixed. so it;s a string
I tried parseFloat thing already, but see the last part of my question, I updated, that happens when I use parseFloat, when I type 545, or 555 it works, but when I type 565 or 575, again returns true.
No when you use parseFloat for both operands its working fine. I have tested different combinations.
|
0

If I'm understanding you correct:

val = 11;

max = 10000;

if(val > max)

if(11 **is greater than** 10000)

It should return false. 11 is not higher than 10,000.

5 Comments

It should be a comment
@mohamedrias Genuinely curious, why is that? Many times have I asked a question where the issue is a simple syntax error or misunderstanding of my own code, where people have posted answers which I have accepted.
I understand. I thought that the OP's original question may be different. Because that's obvious that 11 > 10000 is always false. So only mentioned. But great to see you explaining it there. Keep up
@Matt This is not at all a syntax error, I am testing my code with every possibility, I want that you read my question again, and then see if you can help me solve this.
@Matt What you saying in your answer is correct, it should return false, but it is returning true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.