Xamarin Ios String Format issue

Attila Cser 21 Reputation points
2021-04-11T16:30:12.647+00:00

Hi,

We have encountered an issue in our mobile app displaying a formatted string. This happens only in iPhone. On Android it works perfectly.
The code is like:

        public string FormatHours(double hours)
        {
            int actualHours = (int)hours;
            int actualMinutes = (int)((hours - Math.Truncate(hours)) * 60);

            return string.Format(TranslationService.Get("FormatString.Time"), actualHours, actualMinutes);
        }

This is defined in our viewmodel, and we use that property normally via data-binding.
The FormatString.Time value is: hh\h:mm\m

In the Android version the time is correctly formatted like: 4h 16m
But in Ios we show the string format placeholder all the time like: hh\h:mm\m

Any idea what might be the problem?

Tried on different locales, but same issue.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-04-12T09:06:54.367+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I guess the string format is invalid on iOS .

    You can simply combine the string manually as below .

       public string FormatHours(double hours)  
               {  
                   int actualHours = (int)hours;  
                   int actualMinutes = (int)((hours - Math.Truncate(hours)) * 60);  
         
                   return actualHours + "h " + actualMinutes + "m";  
         
               }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful