Coder Social home page Coder Social logo

Comments (2)

heretic-G avatar heretic-G commented on May 28, 2024
function splitNum (data: unknown): [string, string] {
    if (typeof data === 'number' && !isNaN(data)) {
        const dataStr = data.toString()
        const eIndex = dataStr.indexOf('e-')
        if (eIndex > -1) {
            const result = dataStr.split('e-')
            return ['0', `0.${'0'.repeat(Number(result[1]) - 1)}${result[0].replace('.', '')}`]
        } else {
            const result = dataStr.split('.')
            if (result.length === 1) {
                result.push('0')
            }
            return [result[0], `0.${result[1]}`]
        }
    }
    return ['0', '0']
}

from daily-question.

Feahter avatar Feahter commented on May 28, 2024
/**科学计数法兼容*/
const SNFn = (val: string) => {
  let state = val.includes('e-');
  let splitStr = `e${state ? '-' : '+'}`;
  let data = val.split(splitStr);
  let fixBit = Number(data[1]);
  let temp = data[0];
  if (temp.includes('.')) {
    let poinitPosition = temp.indexOf('.');
    let len = temp.length;
    fixBit = fixBit + (poinitPosition + (state ? 0 : 1)) - len;
    temp = `${temp}`.replace('.', '');
  }
  let result = state
    ? `0.${`0`.repeat(fixBit)}${temp}`
    : `${temp}${`0`.repeat(fixBit)}`;
  if (state && result.includes('-')) {
    result = `-${result.replace('-', '')}`;
  }
  return state ? [`0`, result] : [result, `0`];
};

/**main*/
const splitFn = (val: number) => {
  const valStr = `${val}`;
  /**完全整数返回*/
  if (!valStr.includes('.') && !valStr.includes('e')) return [valStr, `0`];
  /**科学计数法兼容*/
  if (valStr.includes('e')) {
    let result = SNFn(valStr);
    return result;
  }
  /**正常含小数返回*/
  const result = valStr.split('.');
  let state=result[0].includes('-')
  return [`${result[0]}`, `${state?'-':''}0.${result[1]}`];
};

https://stackblitz.com/edit/typescript-hr1bbp

from daily-question.

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.