Search code examples
c#asp.netstringculturecurrentculture

How to change the culture of a number in C#?


I'm sending parameters to paypal as hidden form vars.

but my site's culture is Danish. So "50,00" is the value for "amount_1"

<input type="hidden" name="amount_1" value="50,00" />

I'm using this code that converts the 50 to "50,00"

item.PricePaid.ToString("#.00")

I believe the number should be like: "1234.56" Is there a way to set the culture to en-US just on this process? (not side wide) Or a better way to do this?

thanks!


Solution

  • You could use the overload of ToString that takes an IFormatProvider and then use GetCultureInfo to pass in the required culture info:

    item.PricePaid.ToString("#.00", CultureInfo.GetCultureInfo("en-US"));
    

    Alternatively, you could (probably) specify the invariant culture rather than "en-US":

    item.PricePaid.ToString("#.00", CultureInfo.InvarianCulture);