Вопрос

For example:

If the Byte value is 5, I would like it to be displayed as 005.

If the Byte value is 10, I would like it to be displayed as 010.

Basically, I always want 3 digits. 119 would remain 119.

I'm aware of .padleft, but do not want to use an if statement. I tried Format("D3") with no luck.

Any suggestions would be appreciated.

Это было полезно?

Решение

If you are using String.Format, you need to specify the index for the parameter, not just D3, so it would be something like this:

Dim r As Byte = 10
Console.WriteLine(String.Format("{0:D3}", r))

Alternatively, you could just use D3 into the ToString of r:

Dim r As Byte = 10
Console.WriteLine(r.ToString("D3"))

Другие советы

You can use String.Format to specify the number of digits you want in the output:

Dim b As Byte = 5
Debug.WriteLine(String.Format("{0:000}", b))

Or use this in the .ToString overload like this:

Debug.WriteLine(b.ToString("000"))

in both cases the output is 005

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top