Advanced-Frontend / Daily-Interview-Question

我是依扬(木易杨),公众号「高级前端进阶」作者,每天搞定一道前端大厂面试题,祝大家天天进步,一年后会看到不一样的自己。
https://muyiy.cn/question/
27.42k stars 3.29k forks source link

第 143 题:将'10000000000'形式的字符串,以每3位进行分隔展示'10.000.000.000',多种实现方式 #296

Open zhoushaw opened 5 years ago

first12 commented 5 years ago

var str = '10000000000'; var arr = []; for(var i=0; i<Math.round(a.length/3); i++){ arr.push(str.substring(str.length-3(i+1), str.length-i3)); } arr.reverse(); arr.join('.')

kavience commented 5 years ago
const splitStr = (str, point = 3, split = '.') => {
    let newStr = '';
    const reverseStr = str.split('').reverse().join('');
    for (const s in reverseStr) {
        newStr = (s !== '0' && s % point === 0) ? newStr + split + reverseStr[s] : newStr + reverseStr[s];
    }
    return newStr.split('').reverse().join('');
}
lhyt commented 5 years ago

如果一行写不出来,我选择不写

// 德国以 . 分割金钱, 转到德国当地格式化方案即可
10000000000..toLocaleString('de-DE') 

// 寻找字符空隙加 .
'10000000000'.replace(/\B(?=(\d{3})+(?!\d))/g, '.')

// 寻找数字并在其后面加 . 
'10000000000'.replace(/(\d)(?=(\d{3})+\b)/g, '$1.')
ChuYang-FE commented 5 years ago

var myNum = Number('10000000000'); //字符串转数字后就可以使用toLocaleString()啦~

var num2Str = myNum .toLocaleString(); //"10,000,000,000"

num2Str.replace(/,/g, "."); //"10.000.000.000"

哭了,这么好的方法竟然这么多人踩

phongm commented 5 years ago

应该是考金额的转换吧,考虑到小数点的情况,如下: '10000000000.0000'.replace(/\d{1,3}(?=(\d{3})+(.\d*)?$)/g, '$&,')

zhenbushidashen commented 5 years ago

笨方法

function addCommaIntoStringNumber(sNumber, splitNumber = 3){
  if (sNumber.length <= splitNumber) {
      return sNumber
  }
  let result = []
  let sNumberQuene = sNumber.split('')
  let index = 1
  let sNumberLength = sNumber.length
  while(sNumberLength--) {
    result.unshift(sNumberQuene.pop())
    if (index++ === splitNumber) {
        result.unshift(',')
        index = 1
    }
  }
  return result.join('')

}
HEternally commented 5 years ago

类似金额的转化,这里相对还要简单一些 '10000000000'.replace(/(?!^)(?=(\d{3})+$)/g, '.'); (?!^)的目的是不匹配开头,虽然这里刚好不会匹配到开头; 但是可以不增加(?!^)试试字符串'100000000000'

wendyjw commented 5 years ago

如果主要想考数字的千位分隔符表示法,可以使用正则 var str= '10000000000'; var reg=/(?!^)(?=(\d{3})+$)/g; str.replace(reg, '.') // "10.000.000.000"

ch8839 commented 5 years ago
str.split("").reverse().reduce((prev, cur, index) => (index + 1) % 3 == 0 ? '.' + cur + prev : cur + prev)
nowherebutup commented 5 years ago
// 正则不大会...只好一步步来
  function foo(num) {
    const arr = [...num].reverse();
    const rst = arr.reduce((init, ele, i) => {
      i % 3 === 2 ? init.push(ele, '.') : init.push(ele);
      return init;
    }, []);
    return rst.reverse().join('')
  }
weiweixuan commented 5 years ago
//  保留两位小数后添加千分位符
 getEffective = str => {
        let [int, decimal] = String(Number(str).toFixed(2)).split('.')
        let res =
          [...int]
            .reverse()
            .reduce(
              (prev, item, index) => (
                prev.push(index % 3 === 2 ? ',' + item : item), prev
              ),
              []
            )
            .reverse()
            .join('') + `.${decimal}`
        if (res[0] === ',') {
          return res.slice(1)
        }
        return res
      }
pangxieju commented 5 years ago

var str= '10000000000'; new Intl.NumberFormat().format(str); // "10,000,000,000"

qq1037305420 commented 5 years ago
"1000000000000".split('').reverse().reduce((total, each, i)=>{
        total.push(each)
        if ((i + 1) % 3 == 0) total.push('.')
        return total
}, []).reverse().join('')
wmui commented 5 years ago
new Intl.NumberFormat().format(100000000000).replace(/,/g, '.');
blockmood commented 5 years ago
console.log('100000000000'.replace(/(?!^)(?=(\d{3})+$)/g,','))
NewPrototype commented 5 years ago
const getStr=(str,splitNum=3,split=',')=>{
    return str.split('').reverse().map((v,i)=>{
            if(i&&i%splitNum==0){
                return `${v}${split}`
            }
            return v
    }).reverse().join('');
}

getStr('10000000000',3,',');
githubcjx commented 5 years ago
var reg = /(\B)(?=(\d{3})+$)/g;
console.log("1000000000".replace(reg, '.'));  // 1.000.000.000
IAMSBLOL commented 5 years ago

'10000000000'.replace(/(?!^)(?=(\d{3})+$)/g,'.')

fxxlsc commented 5 years ago

str.split("").reverse().reduce((prev, cur, index) => (index + 1) % 3 == 0 ? '.' + cur + prev : cur + prev) 上面没有考虑索引+1正好等于长度的时候. 此题str多加一个字符串就会有问题 image

fansOnly commented 5 years ago

str.replace(/\d(?=(\d{3})+$)/g, '$&.');

Yweimao commented 4 years ago
"1000000000000".split('').reverse().reduce((total, each, i)=>{
        total.push(each)
        if ((i + 1) % 3 == 0) total.push('.')
        return total
}, []).reverse().join('')

有没有考虑到1刚好可以除尽的情况, 就会 .100.000.000.00

zhyu512 commented 4 years ago

如果一行写不出来,我选择不写

// 德国以 . 分割金钱, 转到德国当地格式化方案即可
10000000000..toLocaleString('de-DE') 

// 寻找字符空隙加 .
'10000000000'.replace(/\B(?=(\d{3})+(?!\d))/g, '.')

// 寻找数字并在其后面加 . 
'10000000000'.replace(/(\d)(?=(\d{3})+\b)/g, '$1.')

哪位大神能帮忙解释一下第一种方法为什么有两个点..

Reaper622 commented 4 years ago

如果一行写不出来,我选择不写

// 德国以 . 分割金钱, 转到德国当地格式化方案即可
10000000000..toLocaleString('de-DE') 

// 寻找字符空隙加 .
'10000000000'.replace(/\B(?=(\d{3})+(?!\d))/g, '.')

// 寻找数字并在其后面加 . 
'10000000000'.replace(/(\d)(?=(\d{3})+\b)/g, '$1.')

哪位大神能帮忙解释一下第一种方法为什么有两个点..

因为数字后面接第一个.是会被认为是小数点的,所以就变成了10000000. 之后连接一个toLocaleString('de-DE'),接第二个点才被认为是对这个数字字面量进行操作。

wubin1998 commented 4 years ago
let str = '1000000000000'

let result = [...str].reverse().map( (v, i) => {
  if (i % 3 === 0 && i !== 0) {
    return `${v}.`
  }
  return v
}).reverse().join("")
bbrucechen commented 4 years ago

g, "."); //"10.000.000.000"

应该是酸你想得出来自己想不出来所以就踩了哈哈

pikaOuba commented 4 years ago

'10000000'.replace(/(?!^)(?=(\d{3})+$)/g, '.')

glorydayss commented 4 years ago

Number('1000000000').toLocaleString().replace(/,/g, '.')

NewPrototype commented 4 years ago

不会正则就用粗旷的方式吧

'1000000000000'.split('').reverse().map((v,i)=>{
  return i%3==2?`,${v}`:v
}).reverse().join('')
FFFangYu commented 4 years ago
// 正则不大会...只好一步步来
  function foo(num) {
    const arr = [...num].reverse();
    const rst = arr.reduce((init, ele, i) => {
      i % 3 === 2 ? init.push(ele, '.') : init.push(ele);
      return init;
    }, []);
    return rst.reverse().join('')
  }

'100000000'会输出'.100.000.000' 还需要加个判断 i % 3 === 2&&i!==(arr.length-1) ? init.push(ele, '.') : init.push(ele);

boboyangmoumou commented 4 years ago

let reg = /(?!^)(?=(\d{3})+$)/g '10000000000'.replace(reg, ',')

robynluo commented 4 years ago

Number(100000000000).toLocaleString('de-DE')

CHENJIAMIAN commented 4 years ago
87463297 =>"87,463,297"
    "87463297".replace(  /\d{1,3}(?=(\d{3})+$)/g  ,  (s)=>s+',')
        /\d{1,3}(?=(\d{3})+$)/g ,//找出后面跟有三个数字的,即["87", "463"],添加逗号在后面即可
            \d{1,3} //1-3位数字
            (?=(\d{3})+$) //?=表示后面要跟着符合(\d{3})+$的, 即1个或多个(3个数字)结尾的
jefferyE commented 4 years ago
// 从后向前遍历,每次截取三个字符,直到完成
const str = '10000000000';
const arr = [];
for (let len = str.length, i = len - 1; i >= 0; i = i - 3) {
  let start = i - 2;
  if (start < 0) start = 0;
  arr.push(str.slice(start, i + 1));
}
return arr.reverse().join('.');
jiashicheng commented 4 years ago

'10000000000.0000'.replace(/\d{1,3}(?=(\d{3})+(.\d*)?$)/g, '$&,')

笑死我了,运行过吗?

jackchang2015 commented 4 years ago
str.split("").reverse().reduce((prev, cur, index) => (index + 1) % 3 == 0 ? '.' + cur + prev : cur + prev)

有点问题 '100000000'.split("").reverse().reduce((prev, cur, index) => (index + 1) % 3 == 0 ? '.' + cur + prev : cur + prev) => //".100.000.000"

xueshuai commented 4 years ago
const thousandSeparator = (num) => {
  let tstr = num.toString()
  let res = ''
  let count = 0
  for (let i = tstr.length - 1; i >= 0; i--) {
    res += tstr[i]
    ++count
    if (count % 3 === 0 && i !== 0) {
      res += '.'
    }
  }
  res = res.split('').reverse().join('')
  return res
}
yuXi111 commented 4 years ago

考虑符号和小数位,用的笨办法

const thousands = (num) => {
  let suff;
  let pre;
  let symbol='';
  if(num){
    num = num +'';
    [pre,suff] = num.split('.')
    if(pre.length <= 3) return num;
    if(isNaN(pre.slice(0,1))){
      symbol = pre.slice(0,1);
      pre = pre.slice(1);
    }
    pre = pre.split('').reverse();
    for(let i in pre){
      if(i>1 && i%3 === 0){
        pre[i] = pre[i]+',';
      }
    }
    return symbol+pre.reverse().join('')+(suff ? ('.'+suff):'');
  }
  return num;
}
ghost commented 3 years ago
const regex = /(?<!^)(?=(\d{3})+$)/g;
const s = '123456789';
s.replace(regex, ',');
pan-Z2l0aHVi commented 3 years ago
/**
 * @param {String} str
 * @return {String}
 */
function separate(str) {
  const arr = str.split('')
  for (let i = arr.length - 3; i > 0; i -= 3) {
    arr[i] = `.${arr[i]}`
  }
  return arr.join('')
}

const res = separate('100000000')
console.log('res: ', res)
zjkblade commented 3 years ago

'10000000000' .split('') .reverse() .join('') .replace(/(\d{3})/g,'$1.') .split('') .reverse() .join('')

zjkblade commented 3 years ago

Number('100000000').toLocaleString()

Luz-Liu commented 3 years ago
"1000000000000".split('').reverse().reduce((total, each, i)=>{
        total.push(each)
        if ((i + 1) % 3 == 0) total.push('.')
        return total
}, []).reverse().join('')

有没有考虑到1刚好可以除尽的情况, 就会 .100.000.000.00

可以这么写 "1000000000000".split("").reverse().reduce((prev, next, index) => { if(index > 0 && index % 3 == 0) { return next = next + '.' + prev } else { return next + prev } }, '')

Luz-Liu commented 3 years ago
"1000000000000".split("").reverse().reduce((prev, next, index) => {
if(index > 0 && index % 3 == 0) {
return next = next + '.' + prev
} else {
return next + prev
}
}, '')
XW666 commented 3 years ago

const fond07 = (str) => { let defaultStr = '' if (str !== null && str !== '' && str !== undefined) { defaultStr = str.replace(/(\d)(?=(\d{3})+\b)/g, '$1.') } return defaultStr }

ShaneQin commented 3 years ago

考虑数字是三的倍数情况

function splitNum(str) {
    return str.replace(/(\d)(?=(\d{3})+$)/g, '$1.');
  }
2214962083 commented 3 years ago
const transStr = str => str.split('')
                       .reduceRight((result, word, index, arr) => 
                                         ((index + 1) % 3 === 0  && index !== arr.length - 1  ?  '.'  :  '' ) 
                                         + word + result)
poppydani commented 3 years ago
function format(str) {
  const reverse = str.toString().split('').reverse().join('');
  return reverse.replace(/(\d{3})/g, '$1,').replace(/\,$/g, '').split('').reverse().join('');
}
LFdeWeb commented 2 years ago

'-1019234801211.2142345678'.replace(/(?<!...)(\d)(?=(\d{3})+($|.))/g, '$1,')

Athrun-Judah commented 2 years ago

ts的实现方法:

方法一(推荐):

type SplitStr<
  Str extends string,
  Ele extends string = '.'
> = Str extends `${infer First}${infer Second}${infer Third}${infer Rest}`
      ? `${First}${Second}${Third}${Ele}${SplitStr<Rest>}`
      : Str

type ReverseStr<
  Str extends string,
  Result extends string = ''
> = Str extends `${infer First}${infer Rest}`
      ? ReverseStr<Rest , `${First}${Result}`>
      : Result

type test= ReverseStr<SplitStr<ReverseStr<'10000000000'>>>

let a:test = '10.000.000.000'   //代码提示即为所求结果

方法二:

type SplitStr<
  Arr extends string,
  Ele extends string = '.',
  LimitLength extends number = 3, 
> = Arr extends `${infer First}${infer Second}${infer Rest}` | `${infer Left}${infer Right}`
  ? Divide<Subtract<StrLen<Arr> , 1> , LimitLength> extends never
    ? Divide<Subtract<StrLen<Arr> , 2> , LimitLength> extends never
      ? AddPoint<Arr , Ele>
      : `${First}${Second}${Ele}${AddPoint<Rest>}`
    : `${Left}${Ele}${AddPoint<Right>}`
  : never

type AddPoint<
  Str extends string ,
  Ele extends string = '.'
> = Str extends `${infer First}${infer Second}${infer Third}${infer Rest}`
      ? StrLen<Rest> extends 0
        ? Str
        : `${First}${Second}${Third}${Ele}${AddPoint<Rest , Ele>}`
      : Str

type BuildArray<
  Length extends number,
  Ele = unknown,
  Arr extends unknown[] = []
> = Arr['length'] extends Length
      ? Arr
      : BuildArray<Length , Ele , [...Arr , Ele]>

type Subtract<Num1 extends number , Num2 extends number> = 
  BuildArray<Num1> extends [...arr1: BuildArray<Num2> , ...arr2: infer Rest]
    ? Rest['length']
    : never

type Divide<
  Num1 extends number,
  Num2 extends number,
  CountArr extends unknown[]=[]
> = Num1 extends 0? CountArr['length']
    : Divide<Subtract<Num1, Num2> , Num2 , [unknown, ...CountArr]>

type StrLen<
  Str extends string,
  CountArr extends unknown[]=[]
> = Str extends `${string}${infer Rest}`
    ? StrLen<Rest , [...CountArr , unknown]>
    : CountArr['length']

type test = SplitStr<'10000000000'>

let a:test = '10.000.000.000'   //代码提示即为所求结果
Yangfan2016 commented 2 years ago

正则


'10000000000'.replace(/(\d)(?=(\d{3})+$)/g, '$1.')

国际化


new Intl.NumberFormat('de-DE').format(100000000000)

toLocaleString

100000000000..toLocaleString('de-DE')