简体   繁体   中英

format value when show and when edit input

样本格式

I want to format decimal numbers

800000   -> 800,000 

1000.25  -> 1,000.25     
1000.254 -> 1,000.25

1000.255 -> 1000.26

I do not know how to do it, I am using bootstrap-table and x-editable .

You can create a function to format number like the following:

 function numberFormatter(n){ n = (Math.round(n * 100) / 100).toFixed(2).toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); return n.split('.')[1] == 0 ? n.split('.')[0] : n; } console.log(numberFormatter(800000)) console.log(numberFormatter(1000.25)) console.log(numberFormatter(1000.254)) console.log(numberFormatter(1000.255)) 

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