-1

I have a response which is sending me a value of 0.0554

And the following is my code to convert it into a percentage and display it.

  var m_e = JSON.parse(JSON.stringify(data)).moe;
  console.log(m_e); //here I am getting 0.0554
  var e = m_e*100;
  console.log(e); //here I am getting 5.539999999999999
  $('#element').html('±' + e + '%');

And as a result I am getting this enormous value. But I want to print 5.54 only.

Where am I going wrong?

Any kind of help will be greatly appreciated. Thanks.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Steve Doson
  • 703
  • 4
  • 16
  • 34

1 Answers1

1

You can use toFixed(2) to get two digits after the decimal point:

var data = {
  "moe" : 0.0554
}

var m_e = JSON.parse(JSON.stringify(data)).moe;
  console.log(m_e); 
  var e = m_e*100;
  console.log(e); 
  $('#element').html('±' + e.toFixed(2) + '%');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='element'></span>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62