Xamarin.Forms中的标签StringFormat 1000000到1.000.000

问题描述 投票:0回答:1

我有<Label Text="{Binding Item.O_SALDO , StringFormat = ' {0:N0} '}" TextColor="#006BE6" Font="Bold, 16" Grid.Row="1" HorizontalOptions="End" />

并且我的输出是ex 1,000,000,但我想使用stringformat 1.000.000

c# xamarin.forms string-formatting
1个回答
0
投票

有一个实现者的解决方案。

Xaml代码如下:

<Label BackgroundColor="AliceBlue" 
        Text="{Binding ModelValue}" HorizontalOptions="CenterAndExpand"  FontSize="Large" TextColor="Black" />

创建一个将int值转换为所需格式string的方法:

private string ConvertIntToFormatSrting(int num)
{
    NumberFormatInfo nfi = (NumberFormatInfo)
    CultureInfo.InvariantCulture.NumberFormat.Clone();
    nfi.NumberGroupSeparator = ".";

    string result = number.ToString("N0");

    return result;
}

因此在MainPage中:

public string ModelValue { set; get; }
private int number { set; get; }

public MainPage()
{
    InitializeComponent();
    number = 1000000;

    ModelValue = ConvertIntToFormatSrting(number);

    Console.WriteLine("------"+ ModelValue);

    BindingContext = this;
}

[:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.