views:

136

answers:

5

Hi, i have a issue with some string formats, i'm forcing the culture when formatting specific formats:

get { return String.Format("{0:###,###,###,###,##0}", Convert.ToDecimal(_monthPay, new System.Globalization.CultureInfo("es-ES"))); }

so that i can get this:

$300.000,01

On localhost it works fine, but when i publish to the server, i get this:

$300,000.01

I don't know why!!! I don't have access to the server, so I can't change the regional settings on the server; is there another way to solve it? so that i works properly on localhost and when publishing?

Thanks.

A: 

Instead of using the really long custom format, what about the built-in format for currency?

get { return Convert.ToDecimal(_monthPay).ToString("C", new System.Globalization.CultureInfo("es-ES")); }

EDIT: moved the culture info.. I still don't think using some massive format string is right. There are built-in format conventions for currency...

Scott Anderson
@SLaks Don't be so scared to down-vote. Do your part.
Josh Stodola
+1  A: 
Thread.CurrentUICulture = CultureInfo.GetCultureInfo("es-ES");

Try putting that in some initialization block.

Daniel A. White
+5  A: 

You're passing the CultureInfo in the wrong place.

By passing the CultureInfo to Convert.ToDecimal, you're telling Convert.ToDecimal to convert the number using that culture. (This is relevant if _monthPay is a string and needs to be parsed)
However, you didn't pass a CultureInfo to String.Format, so it is still using the default culture.

By the way, you should only use String.Format if you're combining multiple values. In your case, you should call the ToString overload. Also, your format string is needlessly long; you can simply write #,0. If you want to include a currency symbol, you can simply use C instead.

Therefore, you should write Convert.ToDecimal(_monthPay).ToString("#,0", new System.Globalization.CultureInfo("es-ES")).

SLaks
You can also pass the `CultureInfo` as the first argument to `String.Format`: `String.Format(new CultureInfo("es-ES"), "{0:###,###,###,###,##0}", value)`
Phil Ross
Yes, but he shouldn't be using `String.Format` in the first place.
SLaks
Honestly he doesn't need the huge format string either. He can replace it with a single "C" to get a currency format.
Scott Anderson
He doesn't (seem) to want a currency symbol.
SLaks
+1  A: 
get 
{ 
    var culture = new System.Globalization.CultureInfo("es-ES");
    return Convert
        .ToDecimal(_monthPay, culture)
        .ToString("###,###,###,###,##0", culture); 
}
Darin Dimitrov
+1  A: 

What you're doing here is telling the Convert.ToDecimal function what _monthPay will look like. What you're expecting is that the String will be formatted with the culture info.

You should be telling String.Format what culture to use:

String.Format( new System.Globalization.CultureInfo("es-ES"), "{0:###,###,###,###,##0.##}", Convert.ToDecimal(_monthPay)));
Sam T.