Home All Groups Group Topic Archive Search About

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

Author
13 Jun 2009 11:48 AM
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

Author
13 Jun 2009 12:11 PM
Stephany Young
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
Are all your drivers up to date? click for free checkup

Author
13 Jun 2009 3:21 PM
Göran Andersson
AhWau wrote:
Show quoteHide quote
> 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...

--
Göran Andersson
_____
http://www.guffa.com
Author
16 Jun 2009 9:17 PM
Ben Voigt [C++ MVP]
Show quote Hide 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);

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.

Show quoteHide quote
>
> Thank you so much
Author
17 Jun 2009 8:56 AM
Andreas Ott
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
Author
17 Jun 2009 2:12 PM
Ben Voigt [C++ MVP]
Andreas Ott wrote:
Show quoteHide quote
> 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?

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.

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
Author
17 Jun 2009 7:21 PM
Andreas Ott
Hello!
>>
>> --> 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.
>

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

  Greeting Andreas

Bookmark and Share