2

Working on the below, when logging the array 'tips' it seems that the numbers pushed into it have only one decimal point.

Before adding in parseFloat it would return 2 decimal points, however it was returned as a string. Once parseFloat was added it now only seems to return a single decimal point.

Thank you.

var tips = [];
var calculateTip = function(bill) {
  switch (true) {
    case bill < 50:
      tips.push(parseFloat((bill * 0.2).toFixed(2)));
      break;
    case bill >= 50 && bill < 201:
      tips.push(parseFloat((bill * 0.15).toFixed(2)));
      break;
    default:
      tips.push(parseFloat((bill * 0.10).toFixed(2)));
  }
}
calculateTip(124);
calculateTip(48);
calculateTip(268);
console.log(tips);
James Paterson
  • 2,652
  • 3
  • 27
  • 40
Cal
  • 95
  • 1
  • 7
  • as a float, 1.20 doesn't make sense, but as a string you can have 1.20. So You have to go with string if you want two decimal points. – Hamza Arshad Apr 30 '20 at 20:01
  • **Important: if you're dealing with money, you should not be using major units (eg, dollars)**. Computers (base 2) can't represent base 10 very well. Instead do what finance apps do - deal in *minor units* (cents) and then display them as dollars and cents (or whatever else) when it's time to show them. – mikemaccana Apr 30 '20 at 20:06

3 Answers3

3

Number.prototype.toFixed() returns a string representation of the number you call it on with the right number of decimals, but if you parse it back to number with parseFloat, those will go away, as number doesn't care about trailing zeros.

You can fix that by just getting rid of the parseFloat:

const tips = [];

function calculateTip(bill) {
  if (bill < 50)
    tips.push((bill * 0.2).toFixed(2));
  else if (bill >= 50 && bill < 201)
    tips.push((bill * 0.15).toFixed(2));
  else
    tips.push((bill * 0.10).toFixed(2));
}

calculateTip(124);
calculateTip(48);
calculateTip(268);

console.log(tips);
Danziger
  • 19,628
  • 4
  • 53
  • 83
1

Numbers in Javascript are output to console with as many decimal places as needed.

console.log(1.0000001)
console.log(1.1)

All your numbers only display with one decimal point as they only have one decimal place. You should return a string instead if you want to show them with a specific precision.

var tips = [];
var calculateTip = function(bill) {
  switch (true) {
    case bill < 50:
      tips.push((bill * 0.2).toFixed(2));
      break;
    case bill >= 50 && bill < 201:
      tips.push((bill * 0.15).toFixed(2));
      break;
    default:
      tips.push((bill * 0.10).toFixed(2));
  }
}
calculateTip(124);
calculateTip(48);
calculateTip(268);
console.log(tips);
James Paterson
  • 2,652
  • 3
  • 27
  • 40
1

I moved the toFixed method next to your parseFloat and it returned me the result you expected:

var tips = [];
var calculateTip = function(bill) {
    switch(true) {
        case bill < 50:
            tips.push(parseFloat((bill * 0.2)).toFixed(2));
            break;
        case bill >= 50 && bill < 201:
            tips.push(parseFloat((bill * 0.15)).toFixed(2));
            break;
        default:
            tips.push(parseFloat((bill * 0.10)).toFixed(2));
    }
}
calculateTip(124);
calculateTip(48);
calculateTip(217);
console.log(tips);
Mario Perez
  • 2,777
  • 1
  • 12
  • 21