1

I have a javacript output 14.85 now i want to make it 14.90 for 14.85 and for 14.84 i need it to 14.80, Is it possible ?

thanks

Shamim Ahmed
  • 931
  • 2
  • 16
  • 30
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/round (Example: Decimal rounding) –  Mar 24 '13 at 11:39
  • check the answer for that question: http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript – Mohamed Habib Mar 24 '13 at 11:40

3 Answers3

2

How about this:

var number = Math.round(14.85 * 10) / 10;
Amberlamps
  • 39,180
  • 5
  • 43
  • 53
1

Here is the solution that produces the exact result what you need:

function roundFloat(n) {
    return (Math.round(n * 10) / 10).toFixed(2);
}

roundFloat(14.84);  // "14.80"
roundFloat(14.85);  // "14.90"
VisioN
  • 143,310
  • 32
  • 282
  • 281
0
var newNumber = (Math.round(oldNumber*10)/10).toFixed(2);
James Coyle
  • 9,922
  • 1
  • 40
  • 48