GithubHelp home page GithubHelp logo

数据翻牌器BUG about datav-vue HOT 2 CLOSED

pengxiaotian avatar pengxiaotian commented on July 23, 2024
数据翻牌器BUG

from datav-vue.

Comments (2)

ggymm avatar ggymm commented on July 23, 2024

这个工具类也可以实现类似的效果
而且没有使用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.

pengxiaotian avatar pengxiaotian commented on July 23, 2024
  1. 遇到啥精度问题了?举个例子。
  2. 之前有想过用formatNumber,后来放弃了,觉得小数点也加个千分位符可以增强可读性,好吧我后面改一下

from datav-vue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.