如何在 UWP 中为 NumberBox 应用 PercentFormatter? [英] How to Apply PercentFormatter for a NumberBox in UWP?
本文介绍了如何在 UWP 中为 NumberBox 应用 PercentFormatter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在最后添加百分比(0%-100%).它正确地显示了 %,但它像这样在末尾添加了更多的零,
I want to add Percentage at the end(0%-100%). It shows % correctly, but it add more zeros at the end like this,
double number = 75;
NumberBoxnumberBox = new NumberBox { };
PercentFormatter percentFormatter = new PercentFormatter();
percentFormatter.FractionDigits = 0;
percentFormatter.IntegerDigits = 1;
numberBox.NumberFormatter = percentFormatter;
numberBox.Value=number;
推荐答案
这实际上是意料之中的.PercentFormatter 接受任意数字并使用1=100%
的(数学)规则对其进行格式化.所以在你的情况下,75
是 7500%
.为了达到附加%"的效果,对于数字,您可以编写自己的格式化程序并使用它:
This is actually expected. The PercentFormatter takes an arbitrary number and formats it as using the (mathematical) rule that 1=100%
. So in your case, 75
is 7500%
. To achieve the affect of appending a "%" to numbers, you can write your own formatter and use that:
public class AppendPercentageSignFormatter : INumberFormatter2, INumberParser
{
public string FormatInt(long value)
{
return value.ToString() + "%";
}
public string FormatUInt(ulong value)
{
return value.ToString() + "%";
}
public string FormatDouble(double value)
{
return value.ToString() + "%";
}
public long? ParseInt(string text)
{
// Cut off the percentage sign at the end of the string and parse that.
return long.Parse(text.Substring(0, text.Length - 1));
}
public ulong? ParseUInt(string text)
{
// Cut off the percentage sign at the end of the string and parse that.
return ulong.Parse(text.Substring(0, text.Length - 1));
}
public double? ParseDouble(string text)
{
// Cut off the percentage sign at the end of the string and parse that.
return double.Parse(text.Substring(0, text.Length - 1));
}
}
// And later in your UI:
double number = 75;
numberBox.NumberFormatter = new AppendPercentageSignFormatter();
numberBox.Value = number;
这篇关于如何在 UWP 中为 NumberBox 应用 PercentFormatter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文