类似 Ruby 的问题:让这个函数更短(ActionScript 3)
我刚刚编写了一段极其冗长的代码,将 2 这样的数字转换为 02。您能否缩短此函数(维护功能)?
public static function format(n:int, minimumLength:int):String {
var retVal:String = n.toString();
var stillNeed:int = minimumLength - retVal.length;
for (var i:int = 0; i < stillNeed; i++) {
retVal = "0" + retVal;
}
return retVal;
}
请使用变量类型。 如果已经有我不知道的内置功能,则额外加分(良好氛围加分,而不是SO加分)。
如果有人想用其他语言发布一些极其简短的等效内容,那也会很有趣。
I just wrote this incredibly verbose code to turn numbers like 2 into 02. Can you make this function shorter, please (maintaning the functionality)?
public static function format(n:int, minimumLength:int):String {
var retVal:String = n.toString();
var stillNeed:int = minimumLength - retVal.length;
for (var i:int = 0; i < stillNeed; i++) {
retVal = "0" + retVal;
}
return retVal;
}
Please use types for variables. Extra points (good-vibe points, not SO points) if there's already a built-in function that I don't know about.
If anybody wants to post some extremely short equivalent in some other language, that would be fun too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这不是最快的实现(它进行了一些不必要的复制并有一个循环),但它很好并且可读:
This wouldn't be the fastest implementation (it does some unnecessary copying and has a loop), but it is nice and readable:
我不认为有内置的方法,但这可能更干净(如果不一定性能更好):
“zeroes”var消除了循环的需要,只需从预构建的字符串中添加您需要的任意多个零即可。
I don't think there is a built-in way, but this might be cleaner (if not necessarily better performing):
The "zeroes" var eliminates the need for looping, just prepend however many zeroes you need from a prebuilt string.
Christophe Herreman 几乎是对的,但他的方法添加了更多的零,而不是差值。 我修复了一点:
我之前的尝试:
Christophe Herreman almost got it right, but his method adds more zeroes and not the differential amount. I fixed it a bit:
My earlier try:
怎么样:
顺便说一句,没有内置函数可以执行此操作。 如果您需要合适的填充功能,请查看 StringUtils。
How about this:
There is not built-in function to do this btw. If you need decent padding functions, take a look at the StringUtils in Apache Commons Lang.
支持 dirkgently 和所有其他在这里做出回应的人,但显然人们在没有实际尝试代码的情况下就投票了。
dirkgently 的最终函数大部分是正确的,但他的 '>' 需要是“<”。
该函数按预期执行(经过相当彻底的测试):
Props to dirkgently and all others who have responded here, but apparently people are voting up without actually trying the code.
dirkgently's final function is mostly correct, but his '>' needs to be a '<'.
This function performs as desired (tested fairly thoroughly):
在 javascript 中这很容易 - 将其粘贴到浏览器的地址栏
javascript: function zit(n, w) {var z="000000000000000000"; return (z+n).substr(- w);} 警报(zit(567, 9)); 无效(0);
In javascript it is easy - paste this into your browser's address bar
javascript: function zit(n, w) {var z="000000000000000000"; return (z+n).substr(-w);} alert(zit(567, 9)); void(0);
我总是通过获取一个包含所有零的零的最大填充宽度的字符串来完成此操作,然后将要填充的字符串附加到零字符串的末尾,然后使用子字符串来获取右侧的长度数字。
就像是:
I've always done this by taking a string that is the maximum padded width of zeros containing all zeros, then appeneding the string to padded to the end of the zeros string and then using substring to get the right hand Length digits.
Something like:
adobe 推出的 as3corelib 包有一个漂亮的小 NumberFormatter 类,它使用一系列 STATIC 类。 在这种情况下,您可以使用 addLeadingZero 函数。
我包含该函数只是为了显示它的简单性,但我会使用该包而不是使用该函数,因为它提供了许多其他有用的功能,如 StringUtils、加密方法(如 MD5、blowfish 等)。
您可以下载该包 此处
对于新用户,您必须提供该包所在位置的类路径。 导入类而不是使用它们的完全限定类名也是明智的做法。
The as3corelib package put out by adobe has a nice little NumberFormatter class that uses a series of STATIC classes. In this case you could use the addLeadingZero function.
I included the function just to show it's simplicity, but I would use the package instead of yoinking the function because it provides many other useful features like StringUtils, encryption methods like MD5, blowfish, etc.
You can download the package here
For newer users you must provide a classpath to where this package lives. It is also smart to import the classes instead of using their fully qualified class names.