3

So I look for answer similar to mine on slackoverflow didn't find anything similar to my problem. I had an issue that the decimal places for my variable below weren't showing 2 decimal places

1) max (which now work) 2) fees (the problem im having)

so I used toFixed on one of the var Max and it works :). But the thing is for using toFixed, toFixed() specifically turns a number into a string. So once the variable max is already a string, calling ' toFixed() ' on it again is an invalid operation. Cause when I tried to use it on "fees"

 var max = (principal/2)-fees;

From the function, it wouldn't work. Even if I made a new variable like:

var Test = fees.toFixed(2)

it still wouldn't work from the explanation i explain above and using https://repl.it/languages to help me. I found about Math.round(fees) as you see below but it gets rid of the decimal and show the whole number only. (the $8 is the fees variable from the code below.)

enter image description here

Is there some way i can fix the fees variable to show 2 decimal places OR do what im doing below and make a new var = to fees and use some code I never heard of. (still learning about this) thanks

<script>
    function CollFee(input, principal, fees){

        var max = ( (principal/2)-fees ).toFixed(2);
        var test = Math.round(fees);
        //var input = this.value;
        //alert(input);

        if(input.value > max){
            input.value = '';
            var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
            text = text + ' Current fee on account is $' +test+ '. THE Maximum additional fee allowed is $' +max;
            alert(text);
            //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
            //input.value = '';

        }
    };
</script>
1

4 Answers 4

2

You should really separate your logic from your display. .toFixed should only be used when you've finalized your math and are dealing with your strings. A secondary problem you are likely experiencing is (if I assume correctly) that because 'input.value' is a string as well, then the comparing input.value to max will give you incorrect results as well.

Try

function CollFee(input, principal, fees){

    var max = ( (principal/2)-fees );
    var val = parseFloat(input.value)

    if(val > max){
        input.value = '';
        var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
        text = text + ' Current fee on account is $' +fee.toFixed(2)+ '. THE Maximum additional fee allowed is $' +max.toFixed(2);
        alert(text);
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for you help. but i posted the method that works :). thanks for your help
1

It is a little unclear but probably the problem is that you should format your numbers to proper string, AFTER all calculations are done (so during message creation) - so this is probably what you want

function CollFee(input, principal, fees){

  var max = ( (principal/2)-fees );
  var test = Math.round(fees);

  if(input.value > max){
      input.value = '';
      var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
      text =  text + ' Current fee on account is $' 
            + test.toFixed(2) + '. THE Maximum additional fee allowed is $' 
            + max.toFixed(2);
      alert(text);
      //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
      //input.value = '';

  }
    };
Type number and press enter (principal=101, fee=33) (if you type 100 you will see alert)<br>
<input type="text" onchange="CollFee(this, 101,33)"/>

Comments

0

A simple alternative to .toFixed() is to multiply, round down, and divide, like:

let num = 65.4321;
let bigNum = num * 100;
let wholeNum = Math.floor(bigNum);
let currencyFormattedNum = wholeNum/100;
console.log(currencyFormattedNum); // Logs 65.43

You can find more details here: https://javascript.info/number

Comments

0

I really appreciate all your answer and helpful advice to solve this issue in the future, but none of your methods work, because the pop out didnt show up. I experiment and found this to work. Just to show to you guys thanks.

<script>
    function CollFee(input, principal, fees){

        var Smax = ( (principal/2)-fees ).toFixed(2);
        var max = ( (principal/2)-fees )
        var FeeroundP = fees * 1;
        var Feeround = FeeroundP.toFixed(2);
        //var input = this.value;
        //alert(input);

        if(input.value > max){
            input.value = '';
            var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
            text = text + ' Current fee on account is $' +Feeround+ '. THE Maximum additional fee allowed is $' +Smax;
            alert(text);
            //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
            //input.value = '';

        }
    };
</script>

Comments

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.