471,348 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Format decimal thousands

Hi all,
with this code:

decimal? test = 1200345.56m;
decimal? test2 = Decimal.Parse(String.Format("{0:0,0.000}", test));

i can not obtain a decimal value with thousands separators (but without
Decimal.Parse do).

How can i solve?

Thanks in advance
--
Luigi

Sep 15 '08 #1
3 4558
Can you clarify what you are trying to do? A decimal is just a number -
it never really contains any separators. The string version might, but
you've already demonstrated that with your String.Format and Parse usage.

Marc
Sep 15 '08 #2
On Sep 15, 2:15*pm, Luigi <ciupazNoSpamGra...@inwind.itwrote:
Hi all,
with this code:

decimal? test = 1200345.56m;
decimal? test2 = Decimal.Parse(String.Format("{0:0,0.000}", test));
As you have multiple possible decimal separators [BOTH comma AND DOT
are used in different locales] in your string, you will have to use
the Parse function specifying which particular format applies...
>
i *can not obtain a decimal value with thousands separators (but without
Decimal.Parse do).

How can i solve?

Thanks in advance
--
Luigi
Sep 15 '08 #3
I don't see why... both the Format and Parse will take the culture into
account. In the format string the . and , are palceholders for the
decimal and thousand separators (they aren't used verbatim). So this
code should work "as is":

static decimal? TestRoundtrip()
{
decimal? test = 1200345.56m;
string s = String.Format("{0:0,0.000}", test);
decimal? test2 = Decimal.Parse(s);
Console.WriteLine(test2);
return test2;
}
static void Main()
{
decimal? a, b;
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("fr-FR");
a=TestRoundtrip();
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("en-GB");
b=TestRoundtrip();
Console.WriteLine(a==b); // returns true
}
Sep 15 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by stevek | last post: by
2 posts views Thread by Wilfried Mestdagh | last post: by
1 post views Thread by nic | last post: by
5 posts views Thread by Edward Hartfield | last post: by
4 posts views Thread by toffee | last post: by
2 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
2 posts views Thread by =?Utf-8?B?THVpZ2k=?= | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.