如何格式化声明为 int 的变量?使用ToString("N0")格式?



我有一个变量 App.selectedCardCount 声明为 int?

在此代码中,我在 .ToString 因为它说没有接受 1 个参数的重载方法。App.selectedCardCount不可能为空,所以有没有办法对此进行编码

if (App.selectedCardCount == null)
App.selectedCardCount = App.DB.GetSelectedCardCount();
vm.x = App.selectedCardCount.ToString("N0") + " x ";

我也试过这个:

if (App.selectedCardCount != null)
{
vm.x = App.selectedCardCount.ToString("N0") + "x";
}

到目前为止没有任何效果,所以我将不胜感激任何建议。

尽管您的逻辑确保它在运行时不是null值,但它无法在编译时确定该值。可为空的类型有两个可以利用的属性 - HasValue 和 Value。

if (!App.selectedCardCount.HasValue)
App.selectedCardCount = App.DB.GetSelectedCardCount();
vm.x = App.selectedCardCount.Value.ToString("N0") + " x ";
vm.x = App.selectedCardCount?.ToString("N0") + "x";

或:

vm.x = App.selectedCardCoun.Value.ToString("N0") + "x";

最新更新