1

Problem

I get numbers from 1 to 5 including all possible floating point numbers in between. The output must contain two digits after comma and in case of after-comma digits they need to be rounded down (floor).

Example input and output:

  • 1 -> 1.00
  • 4.3 -> 4.30
  • 1.1000 -> 1.10
  • 1.5999 -> 1.59

My Try

My try is doing a Math.floor on the 100x of the number and dividing afterwards to get rid of the unwanted digits after comma. The Number.toFixed(2) gets me the possibly missing zeros afterwards:

(Math.floor(input * 100) / 100).toFixed(2)

The problem with this is JavaScript's floating point inprecision:

Math.floor(4.14 * 100) / 100
// results in 4.13 because 4.14 * 100 is 413.99999999999994
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
  • possible duplicate of [Round to at most 2 decimal places in JavaScript](http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript) – Dylan Corriveau May 19 '15 at 15:57
  • 1
    @DylanCorriveau I don't think it's the same, what Hubert wants is to preserve the 2 significant digits after the decimal without rounding up or down, the other question specifically talks about rounding – Hayko Koryun May 19 '15 at 16:00
  • 1
    I think what you want can only be solved by string manipulation, rounding the numbers to get the 2 significant digits as you see them is finicky – Hayko Koryun May 19 '15 at 16:02
  • 1
    That's because the number which prints as `4.14` is actually 4.13999999999999968025576890795491635799407958984375, see http://www.exploringbinary.com/floating-point-converter/ – Simon Byrne May 19 '15 at 17:01

2 Answers2

2

function formatNumber(x) {
  // convert it to a string
  var s = "" + x;
  // if x is integer, the point is missing, so add it
  if (s.indexOf(".") == -1) {
    s += ".";
  }
 // make sure if we have at least 2 decimals
  s += "00";
  // get the first 2 decimals
  return s.substring(0, s.indexOf(".") + 3);
}

document.write(1 + " -> " + formatNumber(1) + "<br/>");
document.write(4.3 + " -> " + formatNumber(4.3) + "<br/>");
document.write(1.1000 + " -> " + formatNumber(1.1000) + "<br/>");
document.write(1.5999 + " -> " + formatNumber(1.5999) + "<br/>");
document.write(4.14 + " -> " + formatNumber(4.14) + "<br/>");

Here is my attempt, the idea is documented in the code. Of course, there are probably better solutions, but this is a quick & dirty solution.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
0

nowadays it's much simpler.

Use Number.prototype.toFixed(), but the value will round up.

  • 1 -> 1.00
  • 4.3 -> 4.30
  • 1.1000 -> 1.10
  • 1.5999 -> 1.60

Here is official docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

function financial(x) {
  return Number.parseFloat(x).toFixed(2);
}

document.write(1 + " -> " + financial(1) + "<br/>");
document.write(4.3 + " -> " + financial(4.3) + "<br/>");
document.write(1.1000 + " -> " + financial(1.1000) + "<br/>");
document.write(1.5999 + " -> " + financial(1.5999) + "<br/>");
document.write(4.14 + " -> " + financial(4.14) + "<br/>");
stiweb
  • 31
  • 4