Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fabian 68 posts 172 karma points
    Jan 26, 2017 @ 14:08
    Fabian
    0

    Get Property Value for decimal in swedish culture returning 0

    I have a multilingual site and when you switch to the the swedish culture (sv-SE) everywhere where I am using the code below returns 0.

    value = meal.GetPropertyValue<decimal>(alias);
    

    I think its because that the swedish culture doesn't use . as the decimal seperator, more info on this here.

    Isn't there away to tell the GetPropertyValue method to ignore the culture or something?

    As a workaround I had to implement the following code for now but its not that pretty.

    decimal value = property100g.HasValue ? Convert.ToDecimal(property100g.DataValue, CultureInfo.InvariantCulture) : 0;
    

    Does anybody has any solution to this issue?

    Thanks for the help

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jan 26, 2017 @ 17:28
    Jamie Pollock
    0

    Hey Fabian,
    I had a similar issue to this when using a text field.

    It's exactly as you said, using the Swedish culture (eg decimal comma "1,0" is "1.0").

    I've found the best way when dealing with multiple cultures is to have an agreed backoffice format (typically culture invariant which uses decimal points) and then convert that value into the current culture for display purposes.

    Of course, you're free to use whatever culture you want for dashboard values and then once you have the parsed decimal using ToString to display it with the given culture.

    Thanks,
    Jamie

  • Fabian 68 posts 172 karma points
    Jan 27, 2017 @ 08:31
    Fabian
    0

    Hey Jamie,

    Yes in the backoffice everything is being inputted in one format which is culture invariant which uses decimal points, so that is not an issue.

    The issue is that almost always in order to get a property value in umbraco i n order to display it we use the following code:

    value = meal.GetPropertyValue<decimal>(alias);
    

    But now due to the explained issue in my first comment I can't use it. How are you doing in order to get the property value in order to avoid the mentioned issue? Can you post a code example please?

    Thank you very much for your help.

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jan 27, 2017 @ 14:04
    Jamie Pollock
    101

    Hey Fabian, Understood. I meant to post some code before but got sidetracked.

    What I've done in the past is the following:

    var value = meal.GetPropertyValue<string>(alias); // value of say 15.00
    
    var decimal = Decimal.Parse(value, NumberStyles.Any, CultureInfo.InvariantCulture); // Now a decimal of 15.00
    
    decimal.ToString("<YourFormat>"); // With a given format (eg. "F" or "C") this will now display as 15,00 rather than 15.00
    

    It's important to note that functions like TryParse, Parse, ToString use the current thread culture which is set to sv-SE, so setting the Decimal.Parse to invariant will allow you to parse an expected format.

    The key really is to ensure that what you're parsing from is always consistent. What you're displaying isn't so important as the ToString & current culture will handle that.

    I hope this helps.

    Thanks,
    Jamie

  • Fabian 68 posts 172 karma points
    Jan 27, 2017 @ 14:49
    Fabian
    0

    Hi Jamie,

    Thanks a lot for taking your time in order to help me out, really appreciate it.

    One last question if I may, in the umbraco backoffice are you storing the decimal values in a textstring data type or in a decimal data type?

    Thanks

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jan 27, 2017 @ 14:54
    Jamie Pollock
    0

    That'd be dependant on the data type but I'm fairly sure a numeric or textstring is stored as text data. So all GetPropertyValue

    If my answer above help please mark it as the solution :)

  • Fabian 68 posts 172 karma points
    Jan 30, 2017 @ 12:15
    Fabian
    0

    Hi Jamie,

    Thanks a lot for your response, answer was marked as a solution.

    In an ideal world the GetPropertyValue() method would have a parameter in order to tell it to ignore the culture, but till then we make use of this solution.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft