我对 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
发布评论
评论(3)
与所有编程功能一样,它们都是您的朋友。我在 google 上搜索了 padding Zeros javascript,并被定向到此网站。
使用它,您只需生成您的数字标准,然后在存储/输出它之前,您可以通过此函数运行它:
As with all programming functions are your friend. I searched google for padding zeros javascript and got directed to this site.
Using this you would just generate your number standard and prior to storing/outputting it you'd run it through this function:
一句话:
相信我,它将为您节省大量 JavaScript 时间(以及其他语言)。您可以在 http://sprintf.googlecode.com/files/sprintf 中下载该实现-0.7-beta1.js
到今天,此代码现在可以工作,也许您需要更改库的 src 以获得更多更新:
更多信息,此处,此外还有此有用方法的其他实现。
A one liner:
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:
More information, here, plus there are other implementations of this useful method.
为什么不直接将数字的对数(以 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