简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM