Enrique Gil
Enrique Gil

Reputation: 754

String.Format for Phonenumber leading with zeros

Hi I just want to ask about how can i add padding on string.Format so that when I show it , the mask is applied with leading zeros

Heres my c# code

Model.Phone = String.Format("{0:(###) ###-####}", double.Parse(@e.Phone));

Expected result should be

(012) 345-6789

But the results I am getting is

(12) 345-6789

and the leading zero is missing, Hope someone can help me in this problem , Thanks

Upvotes: 1

Views: 1962

Answers (1)

Adil
Adil

Reputation: 148110

You would use 000 instead of ###, read more about format in MSDN article Custom Numeric Format Strings

String.Format("{0:(000) ###-####}", double.Parse(@e.Phone));

Format specifier "0"

Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

Format specifier "#"

Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

Upvotes: 4

Related Questions