001 形式的数字

发布于 2024-11-25 22:50:45 字数 125 浏览 1 评论 0原文

001这样的数字有没有特殊的名称? 例如,数字 20 是 020,1 是 001。
当你不知道某个东西的名字时,很难用 Google 搜索!
因为我已经在浪费你们的时间了,有谁知道一个可以改变的函数吗?数字转换成这种格式。

Is there a special name for numbers in the format of 001.
For example the number 20 would be 020 and 1 would be 001.
Its hard to Google around when you don`t know somethings name!
Since I am already wasting your guys time does any one know a function for changing numbers to this format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风蛊 2024-12-02 22:50:53

试试这个:

Number.prototype.toMinLengthString = function (n) {
    var isNegative = this < 0;
    var number = isNegative ? -1 * this : this;
    for (var i = number.toString().length; i < n; i++) {
        number = '0' + number;
    }
    return (isNegative ? '-' : '') + number;
}

Try this one:

Number.prototype.toMinLengthString = function (n) {
    var isNegative = this < 0;
    var number = isNegative ? -1 * this : this;
    for (var i = number.toString().length; i < n; i++) {
        number = '0' + number;
    }
    return (isNegative ? '-' : '') + number;
}
总攻大人 2024-12-02 22:50:52

它称为填充

It's called padding.

似梦非梦 2024-12-02 22:50:52

好吧,如果您在某些编程语言的上下文中讨论该表示法,则与 20 相反的 020 将是八进制而不是十进制。

否则,您指的是填充。

Well, if you're talking about that notation within the context of certain programming languages, 020 as opposed to 20 would be Octal rather than Decimal.

Otherwise, you're referring to padding.

葬花如无物 2024-12-02 22:50:52

快速的谷歌搜索显示了这段很好的数字填充代码片段:
http://sujithcjose.blogspot.com /2007/10/zero-padding-in-java-script-to-add.html

function zeroPad(num,count)
{
  var numZeropad = num + '';
  while(numZeropad.length < count) {
    numZeropad = "0" + numZeropad;
  }
  return numZeropad;
}

A quick google search revealed this nice snippet of code for Number Padding:
http://sujithcjose.blogspot.com/2007/10/zero-padding-in-java-script-to-add.html

function zeroPad(num,count)
{
  var numZeropad = num + '';
  while(numZeropad.length < count) {
    numZeropad = "0" + numZeropad;
  }
  return numZeropad;
}
日久见人心 2024-12-02 22:50:52

您可以使用一个简单的函数,例如:

function addZ(n) {
  return (n<10? '00' : n<100? '0' : '') + n;
}

或者一个更强大的函数,用您喜欢的任意字符填充左侧,例如

function padLeft(n, c, len) {
  var x = ('' + n).length;
  x = (x < ++len)? new Array(len - x) : [];
  return  x.join(c) + n
}

You can use a simple function like:

function addZ(n) {
  return (n<10? '00' : n<100? '0' : '') + n;
}

Or a more robust function that pads the left hand side with as many of whatever character you like, e.g.

function padLeft(n, c, len) {
  var x = ('' + n).length;
  x = (x < ++len)? new Array(len - x) : [];
  return  x.join(c) + n
}
孤凫 2024-12-02 22:50:51

它被称为左零填充数字。

Its called left zero padded numbers.

咆哮 2024-12-02 22:50:50

我认为这通常被称为“填充”数字。

I think this is usually called "padding" the number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文