Comments (2)
这个工具类也可以实现类似的效果
而且没有使用toFixed函数,避免了精度问题
/**
* 数字格式化
* @param number 数字
* @param decimal 小数位数
* @param dec_sep 小数分隔符
* @param thousands_sep 千分位分隔符
* @returns {string}
*/
export function formatNumber(number, decimal, dec_sep, thousands_sep) {
number = (number + '').replace(/[^0-9+-Ee.]/g, '')
// 数字
const n = !isFinite(+number) ? 0 : +number
// 保留小数位数
const p = !isFinite(+decimal) ? 0 : Math.abs(decimal)
// 千分位分隔符
const t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep
// 小数分隔符
const d = (typeof dec_sep === 'undefined') ? '.' : dec_sep
const toFixedFix = function(n, p) {
return n.toString().replace(new RegExp(`^(.*\\..{${p}}).*$`), '$1')
}
const s = (p ? toFixedFix(n, p) : '' + Math.round(n)).split('.')
const re = /(-?\d+)(\d{3})/
if (t) {
while (re.test(s[0])) {
s[0] = s[0].replace(re, '$1' + t + '$2')
}
}
if ((s[1] || '').length < p) {
s[1] = s[1] || ''
s[1] += new Array(p - s[1].length + 1).join('0')
}
return s.join(d)
}
from datav-vue.
- 遇到啥精度问题了?举个例子。
- 之前有想过用formatNumber,后来放弃了,觉得小数点也加个千分位符可以增强可读性,好吧我后面改一下
from datav-vue.
Related Issues (20)
- 增加页面级数据源配置 HOT 5
- 右下角增加缩略图功能
- 如果和后端对接,需要的接口能否列下,自己可以补充后端 HOT 2
- 迭代器功能说明
- 大佬,什么时候再更新代码啊?等死我了 HOT 2
- 撤销重做功能 HOT 3
- 太牛了,什么时候能把数据源和数据集的功能加上呢,大佬? HOT 4
- 未来迭代计划 HOT 4
- 过滤器保存功能有问题 HOT 4
- 两个组件叠在一起,点击不准 HOT 2
- 柱状图等图表组件增加值标签配置
- 老大更新的速度明显加快了,加油,期待尽快把移动端设计功能开发哦 HOT 1
- 轮播列表 不显示序号列,列宽呈现出问题
- 滚动列表组件能否加上边框的配置呢
- 生产环境如何去掉mock.js,使用自己后端的api地址
- 请问组件间的交互有计划吗? HOT 6
- 我使用大佬你的代码当框架可以吗,会涉及侵权吗 HOT 1
- 项目是否还继续维护呢 HOT 5
- 刚和公司扯皮完裁员的事,这个月先找工作了
- 都TM被裁了,更个锤子。。 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datav-vue.