locked
how to convert ListingInformation.FormattedPrice to decimal for non-US Market

    Question

  • It looks like ListingInformation.FormattedPrice is not well formatted (at least in RU market)

    E.g.

    for Rusisan market we have:
    ListingInformation.FormattedPrice = "р.300.00"
    ListingInformation.CurrentMarket = "RU"

    But following code:

    Decimal.Parse("р.300.00", NumberStyles.Currency | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, new CultureInfo("RU"));

    throws and exception because

    (new CultureInfo("RU")).NumberFormat.CurrencyDecimalSeparator

    is a comma (",") not a dot (".").

    Decimal.Parse("р.300,00", ..., new CultureInfo("RU") );

    and

    Decimal.Parse("300,00 p.", ..., new CultureInfo("RU") );

    works perfectly.

    How correct way to convert ListingInformation.FormattedPrice to decimal for any non-US Market? I need it to calculate discount value between 3 separate inapps and one 3in1 in app offer and show it to user.



    Wednesday, February 19, 2014 8:55 AM

Answers

  • I can't find the Equivalent for CultureInfo.CurrentCulture.UserDefaultCulture in C# classes yet.

    But have working workaround using C++ Win Runtime Component and GetUserDefaultLocaleName() API.

    C++:

    Platform::String^ LocaleHelper::getUserDefaultLocaleName()
    {
    	WCHAR wcBuffer[10];
    	GetUserDefaultLocaleName(wcBuffer, 10);
    	return ref new Platform::String(wcBuffer);
    }
    C#:
    string s = "р.300.00";
    string market = "RU";
    
    string userLocale = new WRComponent.LocaleHelper().getUserDefaultLocaleName();
    
    Decimal value = Decimal.Parse(s.Replace((new RegionInfo(market).CurrencySymbol), ""), NumberStyles.Currency, new CultureInfo(userLocale));

    Friday, February 21, 2014 1:55 PM

All replies

  • We need exclude "RU" region currency symbol and then use CultureInfo.CurrentUICulture (not market's one) to parce the rest and use only NumberStyles.Currency.

                string s = "р.300.00";
                string market = "RU";
                Decimal value = Decimal.Parse(s.Replace((new RegionInfo(market).CurrencySymbol), ""), NumberStyles.Currency, CultureInfo.CurrentUICulture);

    UPDATE:

    It works only if user set Control Panel\All Control Panel Items\Language->Change date, time, or cumber formats->Format: "US".

    It looks like Windows Store uses CultureInfo.UserDefaultCulture, not CultureInfo.CurrentUICulture when format the price.

    but UserDefaultCulture is internal property and how to access it in window store app is an open question...

    Thursday, February 20, 2014 1:02 PM
  • Hi Yuriy,

    Thanks for sharing the solution with MSDN community members. :)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, February 21, 2014 1:10 AM
    Moderator
  • I can't find the Equivalent for CultureInfo.CurrentCulture.UserDefaultCulture in C# classes yet.

    But have working workaround using C++ Win Runtime Component and GetUserDefaultLocaleName() API.

    C++:

    Platform::String^ LocaleHelper::getUserDefaultLocaleName()
    {
    	WCHAR wcBuffer[10];
    	GetUserDefaultLocaleName(wcBuffer, 10);
    	return ref new Platform::String(wcBuffer);
    }
    C#:
    string s = "р.300.00";
    string market = "RU";
    
    string userLocale = new WRComponent.LocaleHelper().getUserDefaultLocaleName();
    
    Decimal value = Decimal.Parse(s.Replace((new RegionInfo(market).CurrencySymbol), ""), NumberStyles.Currency, new CultureInfo(userLocale));

    Friday, February 21, 2014 1:55 PM