1

I've got some JS:

var updateAmount = function (amount, rate) {
    if (rate > 0 && amount.length > 0 && !isNaN(amount) && amount > 0) {
        amount = Math.round(rate * amount * 100) / 100; // multiply rate with amount and then round to 2 decimals
        $('.you-get').show();
        $('#you-get').text(amount + ' ' + $currencyTo.find(':selected').text());
    } else {
        $('.you-get').hide();
    }
};

What I need is a clause that checks if the value generated from amount + $currencyTo.find is a whole number, if it is then add .00 to the end of it.

2
  • amount = Number(amount); rate = Number(rate); will this help ? Commented Feb 25, 2014 at 10:07
  • 1
    Maybe amount.toFixed(2); I'm not sure what you mean by amount + $currencyTo.find as that seams to be a string and not a number since you are appending empty space between + ' ' + Commented Feb 25, 2014 at 10:07

4 Answers 4

5
if(amount === parseInt(amount)) //returns `true` if it's a integer


var updateAmount = function (amount, rate) {
    if (rate > 0 && amount.length > 0 && !isNaN(amount) && amount > 0 && amount === parseInt(amount)) { //Edited this line
        if(amount === parseInt(amount)) amount += '.00'; //Added this line
        amount = Math.round(rate * amount * 100) / 100; // multiply rate with amount and then round to 2 decimals
        $('.you-get').show();
        $('#you-get').text(amount + ' ' + $currencyTo.find(':selected').text());
    } else {
        $('.you-get').hide();
    }
};
Sign up to request clarification or add additional context in comments.

Comments

1

There is .toFixed() method.

amount = Math.round(rate * amount * 100) / 100;
amount = amount.toFixed(2);

Comments

0

Try this :

var updateAmount = function (amount, rate) {
  if (rate > 0 && amount.length > 0 && !isNaN(amount) && amount > 0) {
    amount = Math.round(rate * amount * 100) / 100; // multiply rate with amount and then round to 2 decimals
    $('.you-get').show();
    var val = amount + '' + $currencyTo.find(':selected').text();
    if (val === parseInt(val)){// check if val is an Int
      $('#you-get').text(val.toFixed(2));//add .00 if amount + $currencyTo.find is an Int
    }else{  
      $('#you-get').text(val);
    }
  }else{
    $('.you-get').hide();
  }
};

2 Comments

Hi, thanks, it doesn't seem to add .00 if there's no decimal place in the variable val?
JSfiddle : in this example, toFixed adds .00 on an integer.
0

Can you use the remainder to check if the number is a whole number? E.g.

var val = parseFloat(amount) + parseFloat($currencyTo.find(':selected').text());
if ( val % 1 == 0 ) { /* whole number */ } else {/* not whole number */ }

You could also check that !isNaN(val)

This is essentially the answer suggested from a similar question, see here

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.