|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
String.Format D0, D1, D2, D3 change by variableint var = 1;
int i = 1; string a; if(var == 0) a = String.Format("{0:D0}", i); if(var == 1) a = String.Format("{0:D1}", i); if(var == 2) a = String.Format("{0:D2}", i); the format is variety by (var) can i simply like this? a = String.Format("{0:???}", i, var); Thank you so much One solution is:
a = string.Format("{0:D" + var.ToString() + "}", i); Show quoteHide quote "AhWau" <Ah***@discussions.microsoft.com> wrote in message news:8CA23CD7-F5D8-4C6E-85F2-6EC86EE1B102@microsoft.com... > int var = 1; > int i = 1; > string a; > > if(var == 0) > a = String.Format("{0:D0}", i); > if(var == 1) > a = String.Format("{0:D1}", i); > if(var == 2) > a = String.Format("{0:D2}", i); > > the format is variety by (var) > can i simply like this? > a = String.Format("{0:???}", i, var); > > Thank you so much AhWau wrote:
Show quoteHide quote > int var = 1; You can't use a format inside a format. You can use another > int i = 1; > string a; > > if(var == 0) > a = String.Format("{0:D0}", i); > if(var == 1) > a = String.Format("{0:D1}", i); > if(var == 2) > a = String.Format("{0:D2}", i); > > the format is variety by (var) > can i simply like this? > a = String.Format("{0:???}", i, var); > > Thank you so much String.Format call to format the format string: a = String.Format(String.Format("{{0:D{0}}}", var), i) or simply: a = String.Format("{0:D" + var.ToString() + "}"), i) Note: From C# 3 var is a keyword, so don't use it for variable names...
Show quote
Hide quote
"AhWau" <Ah***@discussions.microsoft.com> wrote in message How about this?news:8CA23CD7-F5D8-4C6E-85F2-6EC86EE1B102@microsoft.com... > int var = 1; > int i = 1; > string a; > > if(var == 0) > a = String.Format("{0:D0}", i); > if(var == 1) > a = String.Format("{0:D1}", i); > if(var == 2) > a = String.Format("{0:D2}", i); > > the format is variety by (var) > can i simply like this? > a = String.Format("{0:???}", i, var); NumberFormatInfo formatControl = new NumberFormatInfo(); formatControl.NumberDecimalDigits = var; a = i.ToString(formatControl); or a = i.ToString("D", formatControl); or a = String.Format(formatControl, "{0:D}", i); The NumberFormatInfo gives you a great deal of control over the output. Show quoteHide quote > > Thank you so much Hello!
> How about this? System.Globalization.NumberFormatInfo formatControl = new > > NumberFormatInfo formatControl = new NumberFormatInfo(); > formatControl.NumberDecimalDigits = var; > a = i.ToString(formatControl); > or > a = i.ToString("D", formatControl); > or > a = String.Format(formatControl, "{0:D}", i); > > > The NumberFormatInfo gives you a great deal of control over the output. > System.Globalization.NumberFormatInfo(); int digits = 3; int zahl = 1; string a; formatControl.NumberDecimalDigits = digits; a = zahl.ToString(formatControl); a = zahl.ToString("D", formatControl); a = String.Format(formatControl, "{0:D}", zahl); a = string.Format("{0:D" + digits.ToString() + "}", zahl); What is the target? 1 --> 001 or --> 1,000? or --> 1.000? Only this works a = string.Format("{0:D" + digits.ToString() + "}", zahl); What is different between string and String? When I should use string and Stringbuilder? Have someone a example? Greeting Andreas Andreas Ott wrote:
Show quoteHide quote > Hello! Which of these did you want? NumberFormatInfo has a lot of different >> How about this? >> >> NumberFormatInfo formatControl = new NumberFormatInfo(); >> formatControl.NumberDecimalDigits = var; >> a = i.ToString(formatControl); >> or >> a = i.ToString("D", formatControl); >> or >> a = String.Format(formatControl, "{0:D}", i); >> >> >> The NumberFormatInfo gives you a great deal of control over the >> output. > System.Globalization.NumberFormatInfo formatControl = new > System.Globalization.NumberFormatInfo(); > int digits = 3; > int zahl = 1; > string a; > > formatControl.NumberDecimalDigits = digits; > a = zahl.ToString(formatControl); > > a = zahl.ToString("D", formatControl); > > a = String.Format(formatControl, "{0:D}", zahl); > > a = string.Format("{0:D" + digits.ToString() + "}", zahl); > > What is the target? > > 1 > > --> 001 > or --> 1,000? > or --> 1.000? properties giving a lot more control than you get in the format string. NumberDecimalDigits might have been the wrong thing to change. If you want to use the local locale's decimal separator, use Thread.CurrentCulture.NumberFormatInfo (or something like that) instead of creating a new NumberFormatInfo object. Show quoteHide quote > > Only this works > a = string.Format("{0:D" + digits.ToString() + "}", zahl); > > What is different between > string and String? > > When I should use string and Stringbuilder? > > Have someone a example? > > > Greeting Andreas Hello!
>> first:>> --> 001 >> or --> 1,000? >> or --> 1.000? > > Which of these did you want? NumberFormatInfo has a lot of different > properties giving a lot more control than you get in the format string. > NumberDecimalDigits might have been the wrong thing to change. > > If you want to use the local locale's decimal separator, use > Thread.CurrentCulture.NumberFormatInfo (or something like that) instead of > creating a new NumberFormatInfo object. > 1,99 ¤ second 1.99 ¤ third 00000456 4. 1,39453 1.39453 Greeting Andreas
Other interesting topics
deleting row from dataset
Playing an .avi file in a C# app. not all code paths return a value? Controlling the properties of a TextBox from a different Form Button Skinnning Garbage collection and other questions I have stripping newlines from xml stuck on operator overloading??? Problem reading special characters into a list box if or statment |
|||||||||||||||||||||||