0

hi m showing some value in ejs and just wants to reduce it, its not copy its about ejs engine at frontend

value showing:

  4.333333333333333

I just wants that it shows like this:

4.3

ejs code:

  <p class="lead"><%= ticket.feedback.avgRating %></p>

how to do this because google is not showing more results about it

dev
  • 3
  • 5
  • 1
    Possible duplicate of [How do you round to 1 decimal place in Javascript?](https://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript) – VLAZ Oct 07 '19 at 11:59
  • its ejs and we have to code at frontend – dev Oct 07 '19 at 12:00
  • 1
    Are you unable to call `toFixed`? I'm very vaguely familiar with EJS but I thought you can still use basic JS functionality like this. – VLAZ Oct 07 '19 at 12:04

3 Answers3

0

You can fix the decimal the value in the ejs expression as it interprets the Javascript code to show the output. You would need to parse it before as it can be passed as String to the template.

<p class="lead"><%=parseFloat(ticket.feedback.avgRating).toFixed(1)%></p>
jmtalarn
  • 1,345
  • 1
  • 12
  • 15
0

You can make use of jquery to ready the current value and change it to one digit after decimal using toFixed

$(function(){
  var val = $('.lead').text();
  val = parseFloat(val).toFixed(1);
  $('.lead').text(val);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="lead">4.3333333</p>
Bhushan Kawadkar
  • 27,908
  • 5
  • 34
  • 57
0

Use toFixed method : toFixed()

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

example :

financial(4.333333333) =====> 4.3

becher henchiri
  • 540
  • 3
  • 10