Can not convert from 'string' to 'System.IFormatProvider'

2

Follow the code (it works):

var teste = 1;
var teste1 = teste.ToString("000000");

Result:

  

000001

Here is another code (not working):

var teste = "1";
var teste1 = teste.ToString("000000");

Result:

  

Can not convert from 'string' to 'System.IFormatProvider'

How can I convert string to ToString with result "000001"?

    
asked by anonymous 26.04.2017 / 23:35

1 answer

4

It does not make sense to convert string to string , already string . Much less you can convert string to ToString which is a method and not a type.

If you want to put zeros in front of the number use PadLeft() .

using static System.Console;

public class Program {
    public static void Main() {
        var teste = "1";
        WriteLine(teste.PadLeft(6, '0'));
    }
}

See running on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .

    
26.04.2017 / 23:40