Jquery在3个数字/数字后添加点

时间:2018-03-13 14:34:43

标签: javascript

我想在3个数字之后添加一个点 我有一个输入结果,其中包含id price

100 = 100
1000 = 1.000
10000 = 10.000
1000000 = 100.000
1000000 = 1.000.000

我需要在没有插件的情况下进行操作

4 个答案:

答案 0 :(得分:4)

你可以试试这个:

<div class="wrap">
  <div class="linear">
    <div class="amntslider" id="amntslider"></div>
    <div class="MarkWrap">
      <div class="l"><span>0 l</span></div>
      <div class="l"><span>25 l</span></div>
      <div class="l"><span>50 l</span></div>
      <div class="l"><span>75 l</span></div>
      <div class="l"><span>100 l</span></div>
      <div class="l"><span>125 l</span></div>
      <div class="l"><span>150 l</span></div>
      <div class="l"><span>175 l</span></div>
      <div class="l"><span>200 l</span></div>
    </div>
  </div>
</div>

答案 1 :(得分:1)

您可以使用函数reduceRight

来使用此替代方法

function format(str) {
  var length = 3,
    separator = ".",
    count = 0,
    result = str.split('').reduceRight((a, c) => {
      if (count === length) {
        a.push(separator);
        count = 1;
      } else count++;
      a.push(c);
      return a;
    }, []).reverse().join('');
    
    return result;
}


console.log(format('100'))
console.log(format('1000'))
console.log(format('10000'))
console.log(format('1000000'))
console.log(format('100000000'))
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

just another way... But it's just for fun. The right answer that I upvote and you should accept is @Alen Nikolov's one. Simply replace with regular expression

  const formatNum = n => Array.from(String(n)).reverse().map((a,i) => 
  {if((i%3==0)&&(i>0))return a+"."; return a;
 }).reverse().join("");
 
  console.log(formatNum(10000000000));
  console.log(formatNum(1000000));

答案 3 :(得分:0)

扩展@Alen Nikolov答案的数字版本的原型。

var yourImage = document.createElement("img");
yourImage.src = "some/dynamic/source.jpg"
document.body.append(yourImage);