【问题标题】:Space as thousands separator for labels空格作为标签的千位分隔符
【发布时间】:2021-01-20 15:22:35
【问题描述】:

我有一个标签,我正在使用绑定和一个 FormatString :

<Label Text="{Binding buying_price , StringFormat='{0} EUR'}">

标签的文本在我的 ViewModel 中绑定到一个 double,我在标签上得到的内容是这样的:10000 EUR,我想要得到的是10 000 EUR,和12 501 005 EUR 例如(没有尾随的 .00)。 我尝试了StringFormat='{0:C2} EUR'StringFormat='{0:N} EUR'StringFormat='{0:n} EUR',但没有一个给我带来好的结果。

【问题讨论】:

标签: xaml xamarin.forms custom-formatting


【解决方案1】:

我没有让它在 xaml 中工作,而当我在后面的代码中使用转换和使用字符串格式时,它作为例外工作:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:thousandsSeparatorConverter x:Key="thousandsSeparator"/>
    </ResourceDictionary>
</ContentPage.Resources>

<StackLayout>
    <!-- Place new controls here -->
    <Label Text="{Binding date, Converter={StaticResource thousandsSeparator}}"  HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
    
</StackLayout>

还有local:thousandsSeparatorConverter

public class thousandsSeparatorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string s = value as string;


        double number = double.Parse(s);

        // Gets a NumberFormatInfo associated with the en-US culture.
        NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

        // Displays the same value with a blank as the separator.
        nfi.NumberGroupSeparator = " ";
        Console.WriteLine(number.ToString("N0", nfi));

        string convertedNumber = number.ToString("N0", nfi);

        return convertedNumber;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

}

【讨论】:

    【解决方案2】:

    尝试使用StringFormat='{}{0:#,##,#}',具体取决于你的文化,如果你得到10,000 EUR而不是10 000 EUR的结果,你可能需要在代码隐藏中更改它,在前者中,是千位分隔符而不是一个逗号。

    您可能需要查看可用的numeric format string 的完整文档。

    对文化有帮助的相关问题:How would I separate thousands with space in C#

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      相关资源
      最近更新 更多