0

I'm showing some value in ejs and just want to reduce it. It's not copy it's about ejs engine at frontend

value showing:

  4.333333333333333

I want it to show like this:

4.3

ejs code:

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

How do I do this? Google is not showing more results about it.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
dev
  • 3
  • 5
  • its ejs and we have to code at frontend – dev Oct 07 '19 at 12:00
  • 2
    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

2

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,513
  • 1
  • 13
  • 16
0
 <p class="lead"><%= ticket.feedback.avgRating.toFixed(1) %></p>
-1

Use toFixed method : toFixed()

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

example :

financial(4.333333333) =====> 4.3

becher henchiri
  • 618
  • 4
  • 10
  • This doesn't show OP how to use this method. The question is about EJS and OP seems not to understand that they can call `.toFixed(1)` inline. – ggorlen Jan 18 '23 at 18:54