将整数格式化为 5 位数字的字符串

发布于 2024-07-08 20:07:09 字数 148 浏览 8 评论 0原文

我需要一个基于整数的字符串,它应该始终有 5 位数字。

示例:

myInteger = 999
formatedInteger = "00999"

在经典 ASP 中执行此操作的最佳方法是什么?

I need to have a string, based on an integer, which should always have 5 digits.

Example:

myInteger = 999
formatedInteger = "00999"

What is the best way of doing this in classic ASP?

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

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

发布评论

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

评论(4

宣告ˉ结束 2024-07-15 20:07:09

为此,您可以使用字符串操作函数。

这假设使用 VBScript 的经典 ASP(答案的原始版本)。

Const NUMBER_DIGITS = 5

Dim myInteger
Dim formatedInteger

myInteger = 999
formatedInteger = Right(String(NUMBER_DIGITS, "0") & myInteger, NUMBER_DIGITS)

这是一个优化版本,封装在一个函数中,提供可变宽度填充:

Const NUMBER_PADDING = "000000000000" ' a few zeroes more just to make sure

Function ZeroPadInteger(i, numberOfDigits)
  ZeroPadInteger = Right(NUMBER_PADDING & i, numberOfDigits)
End Function

' Call in code:

strNumber = ZeroPadInteger(myInteger, 5)

You can use string manipulation functions for this.

This assumes classic ASP with VBScript (original version of the answer).

Const NUMBER_DIGITS = 5

Dim myInteger
Dim formatedInteger

myInteger = 999
formatedInteger = Right(String(NUMBER_DIGITS, "0") & myInteger, NUMBER_DIGITS)

Here an optimized version, wrapped in a function, offering variable width padding:

Const NUMBER_PADDING = "000000000000" ' a few zeroes more just to make sure

Function ZeroPadInteger(i, numberOfDigits)
  ZeroPadInteger = Right(NUMBER_PADDING & i, numberOfDigits)
End Function

' Call in code:

strNumber = ZeroPadInteger(myInteger, 5)
沫尐诺 2024-07-15 20:07:09

我经常看到这样的事情:

function PadNumber(number, width)
   dim padded : padded = cStr(number)

   while (len(padded) < width)
       padded = "0" & padded
   wend

   PadNumber = padded
end function

PadNumber(999, 5) '00999

Something like this is what I've seen most of the time:

function PadNumber(number, width)
   dim padded : padded = cStr(number)

   while (len(padded) < width)
       padded = "0" & padded
   wend

   PadNumber = padded
end function

PadNumber(999, 5) '00999
从来不烧饼 2024-07-15 20:07:09

尝试一下这一行(好吧,有两行可以防止错误):

function padZeroDigits(sVariable, iLength)
    if (iLength <= len(sVariable)) then padZeroDigits = sVariable : exit function : end if
    padZeroDigits = string(iLength - len(sVariable),"0") & sVariable
end function

Try this for a one-liner (well, two with error prevention):

function padZeroDigits(sVariable, iLength)
    if (iLength <= len(sVariable)) then padZeroDigits = sVariable : exit function : end if
    padZeroDigits = string(iLength - len(sVariable),"0") & sVariable
end function
何处潇湘 2024-07-15 20:07:09

真的,你应该问自己为什么想要这个。

如果这是出于显示目的,那么最好在显示时对您的整数应用字符串格式化函数(将会有一个)。

另一方面,如果您需要它进行内部处理,即您总是期望循环中的五位数字或其他什么,但您不期望对值进行算术运算,那么首先将整数转换为字符串,然后进行任何处理。

简而言之,将整数变量转换为字符串并存储在新变量中,然后使用它。

Really, you should ask yourself why you might want this.

If this is for display purposes then it's probably best to apply a string formatting function (there will be one) to your integer, at the point of display.

On the other hand, if you need it for internal processing, i.e. you're always expecting five digits in a loop or whatever, but you're not expecting to do arithmetic on the value, then convert the integer to a string first and then do any processing.

In short, convert the integer variable to a string and store in a new variable, then use that.

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