Using printf i could specify the precision value as an extra parameter using *. Does the same functionality exist in the C# String.Format?
edit: For example:
Console.WriteLine("{0:D*}",1,4); // Outputs 0001
We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character.
String. format returns a new String, while System. out. printf just displays the newly formatted String to System.
No, String.Format does not support the star operator. You'd need to use either string concatenation
Console.WriteLine("{0:D" + myPrecision.ToString() + "}",1);
or nested String.Format
s
Console.WriteLine(String.Format("{{0:D{0}}}", 4), 1);
Formatting the format string should do the trick:
var number = 1;
var width = 4;
Console.WriteLine(String.Format("{{0:D{0}}}", width), number);
It will output 0001
.
Notice how {{
and }}
are used to escape {
and }
in a format string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With