加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:30:07 所属栏目:asp.Net 来源:网络整理
导读:我需要一个基于整数的字符串,它应该总是有5位数. 例: myInteger = 999formatedInteger = "00999" 在经典ASP中这样做的最佳方法是什么? 解决方法 您可以使用字符串操作函数. 这假定使用VBScript的经典ASP(答案的原始版本). Const NUMBER_DIGITS = 5Dim myIn
我需要一个基于整数的字符串,它应该总是有5位数.

例:

myInteger = 999
formatedInteger = "00999"

在经典ASP中这样做的最佳方法是什么?

解决方法

您可以使用字符串操作函数.

这假定使用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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读