如何将javascript函数舍入到1.00而不是0.100?

I have a function formatMoney that converts numbers into thousand separated strings which works close to perfect. I just noticed today that it struggles to round properly as soon as the input number is x.995xxx. It then rounds to 0.100 instead of 1.00.

有任何解决这个问题的方法吗?我几周前从一个SO线程复制了以下函数,但现在找不到了。

n1 = 0.9940
n2 = 0.9950

$('#n1').html(formatMoney(n1, 2))
$('#n2').html(formatMoney(n2, 2))

function formatMoney(n, dp){

  var s = ''+(Math.floor(n)), d = n % 1, i = s.length, r = '';
  while ( (i -= 3) > 0 ) { r = '.' + s.substr(i, 3) + r; }
  return s.substr(0, i + 3) + r +
    (d ? ',' + Math.round(d * Math.pow(10, dp || 2)) : '');
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <html>
  
 <p> Current wrong output: </p> 

<div id="n1">0,99</div>
<div id="n2">0,100</div>

 <p> Desired output: </p> 
 
<div>0,99</div>
<div>1,00</div>

  </html>
</body>

enter image description here