多位数计数器

发布于 2024-10-20 18:04:42 字数 843 浏览 5 评论 0 原文

我对 Javascript 相当陌生,到目前为止,我只需要遵循教程和阅读论坛就可以了,但这确实让我难住了一段时间。

基本上我想要一个包含七位数字的计数器,我发现了一些东西,但没有一个对我来说真正有意义,所以我写了这个:

imgNumber++;
if (imgNumber < 10){function add(number){return '00000' + number}};
if (imgNumber > 10 && imgNumber < 100){function add(number){return '0000' + number}};
if (imgNumber > 100 && imgNumber < 1000){function add(number){return '000' + number}};
if (imgNumber > 1000 && imgNumber < 10000){function add(number){return '00' + number}};
if (imgNumber > 10000 && imgNumber < 100000){function add(number){return '0' + number}};
if (imgNumber > 100000 && imgNumber < 1000000){function add(number){return '' + number}};

据我所知,它有效。我的问题是:您是否预见到这会出现任何问题,如果没有,是否有更清晰的方式来编写所有这些内容?

我将不胜感激所有的回复。

干杯, 科林

I'm fairly new to Javascript and have been able to get by so far by just following tutorials and reading forums but this one really stumped me for a while.

Basically I wanted to have a counter for numbers that contain seven digits, I found a few things but none that really made sense to me so I wrote this:

imgNumber++;
if (imgNumber < 10){function add(number){return '00000' + number}};
if (imgNumber > 10 && imgNumber < 100){function add(number){return '0000' + number}};
if (imgNumber > 100 && imgNumber < 1000){function add(number){return '000' + number}};
if (imgNumber > 1000 && imgNumber < 10000){function add(number){return '00' + number}};
if (imgNumber > 10000 && imgNumber < 100000){function add(number){return '0' + number}};
if (imgNumber > 100000 && imgNumber < 1000000){function add(number){return '' + number}};

It works as far as I can tell. My question is this: Do you foresee any issues with this and if not is there a cleaner way to write all this?

I'll appreciate any and all replys.

Cheers,
Colin

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

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

发布评论

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

评论(3

迷爱 2024-10-27 18:04:42

与所有编程功能一样,它们都是您的朋友。我在 google 上搜索了 padding Zeros javascript,并被定向到此网站

function pad(number, length) {
       var negative = number < 0;
       var str = '' + Math.abs(number);
       while (str.length < length) {
           str = '0' + str;
       }
       if(negative) str = '-' + str;
    return str;
}

使用它,您只需生成您的数字标准,然后在存储/输出它之前,您可以通过此函数运行它:

pad(1,7);

As with all programming functions are your friend. I searched google for padding zeros javascript and got directed to this site.

function pad(number, length) {
       var negative = number < 0;
       var str = '' + Math.abs(number);
       while (str.length < length) {
           str = '0' + str;
       }
       if(negative) str = '-' + str;
    return str;
}

Using this you would just generate your number standard and prior to storing/outputting it you'd run it through this function:

pad(1,7);
仙女山的月亮 2024-10-27 18:04:42

一句话:

var output = sprintf("%07d", 30);

相信我,它将为您节省大量 JavaScript 时间(以及其他语言)。您可以在 http://sprintf.googlecode.com/files/sprintf 中下载该实现-0.7-beta1.js

到今天,此代码现在可以工作,也许您需要更改库的 src 以获得更多更新:

<script type="text/javascript" src="http://sprintf.googlecode.com/files/sprintf-0.7-beta1.js"> </script>

<script type="text/javascript" >
   alert(sprintf("%04d",4));
</script>

更多信息,此处,此外还有此有用方法的其他实现。

A one liner:

var output = sprintf("%07d", 30);

Believe me, it will save you a lot of time in javascript (and in other languages). You can download the implementation in http://sprintf.googlecode.com/files/sprintf-0.7-beta1.js

By today, this code works right now, perhaps you need to change the src of the library for a more updated:

<script type="text/javascript" src="http://sprintf.googlecode.com/files/sprintf-0.7-beta1.js"> </script>

<script type="text/javascript" >
   alert(sprintf("%04d",4));
</script>

More information, here, plus there are other implementations of this useful method.

半窗疏影 2024-10-27 18:04:42

为什么不直接将数字的对数(以 10 为底)作为要添加多少位数字的指示符。

如果 log(N) 介于 0 和小于 1 之间,则 N 是一位数。
如果 log(N) 大于或等于 1 但小于 2,则 N 为两位数,依此类推。

我实际上并没有用 JavaScript 编写代码,所以我不知道您是否可以访问日志函数,但如果您可以,这是一种获取结果的非常简洁的方法。

约翰·多纳

Why don't you just take the log (base 10) of the number as an indicator of how many digits to add.

If log(N) is between 0 and less than 1, N is a one digit number.
If log(N) is greater than or equal to 1 but less than 2, N has two digits, and so forth.

I don't actually code in JavaScript, so I don't know if you have access to a log function, but if you do, this is a very terse way to get the result.

John Doner

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