Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1,384 views
Skip to first unread message

AhWau

unread,
Jun 13, 2009, 7:48:01 AM6/13/09
to
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

Stephany Young

unread,
Jun 13, 2009, 8:11:57 AM6/13/09
to
One solution is:

a = string.Format("{0:D" + var.ToString() + "}", i);


"AhWau" <Ah...@discussions.microsoft.com> wrote in message
news:8CA23CD7-F5D8-4C6E...@microsoft.com...

Göran Andersson

unread,
Jun 13, 2009, 11:21:49 AM6/13/09
to

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

Ben Voigt [C++ MVP]

unread,
Jun 16, 2009, 5:17:33 PM6/16/09
to

"AhWau" <Ah...@discussions.microsoft.com> wrote in message
news:8CA23CD7-F5D8-4C6E...@microsoft.com...

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.

>
> Thank you so much

Andreas Ott

unread,
Jun 17, 2009, 4:56:58 AM6/17/09
to
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

Ben Voigt [C++ MVP]

unread,
Jun 17, 2009, 10:12:55 AM6/17/09
to

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.

Andreas Ott

unread,
Jun 17, 2009, 3:21:59 PM6/17/09
to
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


0 new messages