Git Product home page Git Product logo

Comments (5)

zhou-z-xin avatar zhou-z-xin commented on August 26, 2024
export const addSeparator = (str = '', sep = ',') => {
  str += '';
  const arr = str.split('.'),
    reg = /(?=((?!\b)\d{3})+$)/g
  let integer = arr[0],
    decimal = arr.length > 1 ? arr[0] : '';
  integer = integer.replace(reg, sep);
  return integer + decimal;
}

from blog.

dongyuanxin avatar dongyuanxin commented on August 26, 2024

@zhou-xin-undefined

测试用例中 -10000.23 的输出有问题。

from blog.

ordinaryA avatar ordinaryA commented on August 26, 2024

from blog.

ordinaryA avatar ordinaryA commented on August 26, 2024

大佬

from blog.

zhou-z-xin avatar zhou-z-xin commented on August 26, 2024
const addSeparator = (str = '', sep = ',') => {
  str += '';
  const arr = str.split('.'),
    reg = /(?=((?!\b)\d{3})+$)/g
  let integer = arr[0],
    decimal = arr.length <= 1 ? '' : `.${arr[1]}`;
  integer = integer.replace(reg, sep);
  return integer + decimal;
}

console.log(addSeparator(-10000.23)); // -10,000.23
console.log(addSeparator(100)); // 100
console.log(addSeparator(1234, ";")); // 1;234

上面那段代码逻辑写错了,小数位的值取错了。

from blog.

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.