零填充 [英] Zero fill left

查看:115
本文介绍了零填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将数字填充到一个固定长度的字符串中?
10个字符?


在Sql中我会做点什么喜欢:右(0000000000+

转换(varchar,amt),10)。


有一种简单的方法可以在VB中执行此操作。网络代码?


谢谢,


Tom

解决方案

4月3日上午11点22分,tshad < t ... @ home.comwrote:


有没有办法将数字填充到一个固定长度为
$的字符串中b $ b 10个字符?


在Sql中我会做类似的事情:右(0000000000+

转换(varchar,amt),10) 。


在VB.net代码中有一个简单的方法吗?


谢谢,


Tom



这样的东西?


私有函数Foo(ByVal amt As Integer)As String

Dim s As String = amt.ToString()

如果s.Length< 10然后

返回新字符串(0c,10 - s.Length())& s

Else

抛出新的ArgumentOutOfRangeException(amt,长度

amt不能大于10)

结束如果

结束功能


谢谢,


Seth Rowe


tshad写道:


有没有办法将数字填充到一个固定的字符串中长度的价格是什么?
10个字符?


在Sql中我会做类似的事情:右(0000000000+

转换( varchar,amt),10)。


在VB.net代码中有一个简单的方法吗?


谢谢,


Tom



将值转换为字符串时使用自定义格式字符串。


Dim answer As Integer = 42

Dim out As String = answer.ToString(" 0000000000")


-

G?ran Andersson

_____
http://www.guffa.com


" tshad" < t@home.com写了

新闻:eH ************** @ TK2MSFTNGP02.phx.gbl:


有没有办法将一个数字填充到一个固定的字符串中,这个字符是10个字符的b $ b长度?


在Sql I中会做类似的事情:正确(0000000000+

转换(varchar,amt),10)。


有一种简单的方法可以做到这一点在VB.net代码?



您可以使用ToString(" FormatStringHere")或String.PadRight

函数。


Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?

Thanks,

Tom

解决方案

On Apr 3, 11:22 am, "tshad" <t...@home.comwrote:

Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?

Thanks,

Tom

Something like this?

Private Function Foo(ByVal amt As Integer) As String
Dim s As String = amt.ToString()
If s.Length < 10 Then
Return New String("0"c, 10 - s.Length()) & s
Else
Throw New ArgumentOutOfRangeException("amt", "The length
of amt cannot be greater than 10")
End If
End Function

Thanks,

Seth Rowe


tshad wrote:

Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?

Thanks,

Tom

Use a custom format string when you convert the value to a string.

Dim answer As Integer = 42
Dim out As String = answer.ToString("0000000000")

--
G?ran Andersson
_____
http://www.guffa.com


"tshad" <t@home.comwrote in
news:eH**************@TK2MSFTNGP02.phx.gbl:

Is there a way to zero fill a numeric into a string that is a fixed
lengh of 10 chars?

In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).

Is there an easy way to do this in VB.net code?


You can use either the ToString("FormatStringHere") or the String.PadRight
function.


这篇关于零填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆