Anthony_Z
Anthony_Z

Reputation: 725

Limit decimal places to 1 using jQuery

Either rounded or truncated:

<div class="my-val">1.334</div>
<div class="my-val">12.133</div>

Should display as

1.3

12.1

Upvotes: 1

Views: 30

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try to use the callBack function of text function along with Number.toFixed() to achieve what you want,

$(".my-val").text(function(_,text){
  return parseFloat(text).toFixed(1);
});

DEMO

Upvotes: 1

Related Questions