String.Format D0, D1, D2, D3 change by variable

  • Thread starter Thread starter AhWau
  • Start date Start date
A

AhWau

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 said:
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

You can't use a format inside a format. You can use another
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...
 
AhWau said:
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);

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.
 
Hello!
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?

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 said:
Hello!
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?

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.
 
Hello!
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.

first:
1,99 ¤
second
1.99 ¤
third
00000456
4.
1,39453
1.39453

Greeting Andreas
 
Back
Top