Skip to content Skip to sidebar Skip to footer

Round Just Decimal In Javascript

I have a value like that: 20.93 I'd like to round it to 20.90 How can I do that in Javascript ? Thanks!

Solution 1:

Multiply the number by 10, round it and then divide the number by 10:

var result = Math.round(20.93 * 10) / 10

Solution 2:

I think this should work:

number.toFixed(1);

Solution 3:

var num= 20.93
num = Math.floor(num * 10) / 10; // 20.9
num = Math.ceil(num * 10) / 10;  //21

Solution 4:

I take it that you want the trailing zero. None of the answers give you that. It has to be a String to have the trailing zero.

functionmy_round(x){
  returnNumber(x).toFixed(1) + '0';
}

If you don't care about the trailing zero and you want a Number (not String), then here's another way to round to decimal places in JavaScript. This rounds to decimal place d.

functionmy_round(x, d){
  returnNumber( Number(x).toFixed(d) );
}

You would do

my_round('20.93', 1);

or

my_round(20.93, 1);

Solution 5:

You can settoFixed(1). but set value = 20.96 then you have 21 and not 20.90;

BUT with my function always will be 20.90 than 20.93 like 20.98/97/96/95...

<script>var num = 20.93;

functionvround(num) { // CREATE BY ROGERIO DE MORAESvarDif = (num.toFixed(2)-num.toFixed(1)).toFixed(2); 
    Dif = Dif * 100; 
    if(Dif <= -1) {

    var n = num.toFixed(2) - 0.05;
    vround(n);
     } else {
     var n = num.toFixed(1)+0;
    console.log(n);
     }
}

vround(num);

</script>

I create this function for get you value, but can change, if you wanna more forms.

Post a Comment for "Round Just Decimal In Javascript"