如何将布尔值一般格式化为是/否字符串?

发布于 2024-10-31 21:28:35 字数 200 浏览 1 评论 0原文

我想根据一些布尔变量以不同的语言显示是/否。
是否有一种通用方法可以根据传递给它的区域设置对其进行格式化?
如果没有,除了 boolVar 之外,格式化布尔值的标准方法是什么?资源.是 : 资源.否.
我猜测涉及到 boolVar.ToString(IFormatProvider)
我的假设正确吗?

I would like to display Yes/No in different languages according to some boolean variable.
Is there a generic way to format it according to the locale passed to it?
If there isn't, what is the standard way to format a boolean besides boolVar ? Resources.Yes : Resources.No.
I'm guessing that boolVar.ToString(IFormatProvider) is involved.
Is my assumption correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘亓 2024-11-07 21:28:36

框架本身并没有为你提供这个(据我所知)。在我看来,将 true/false 翻译成 yes/no 并不比其他潜在翻译(例如 on/off选中/未选中只读/读写或其他)。

我想封装行为的最简单方法是创建一个扩展方法来包装您在问题中建议的构造:

public static class BooleanExtensions
{
    public static string ToYesNoString(this bool value)
    {
        return value ? Resources.Yes : Resources.No;
    }
}

用法:

bool someValue = GetSomeValue();
Console.WriteLine(someValue.ToYesNoString());

The framework itself does not provide this for you (as far as I know). Translating true/false into yes/no does not strike me as more common than other potential translations (such as on/off, checked/unchecked, read-only/read-write or whatever).

I imagine that the easiest way to encapsulate the behavior is to make an extension method that wraps the construct that you suggest yourself in your question:

public static class BooleanExtensions
{
    public static string ToYesNoString(this bool value)
    {
        return value ? Resources.Yes : Resources.No;
    }
}

Usage:

bool someValue = GetSomeValue();
Console.WriteLine(someValue.ToYesNoString());
时光是把杀猪刀 2024-11-07 21:28:36

正如其他答案所示,该框架不允许布尔值具有自定义格式化程序。但是,它确实允许数字具有自定义格式。布尔值的 GetHashCode 方法将返回 1(表示 true)和 0(表示 false)。

根据 MSDN 自定义数字格式字符串,当“;”有 3 个部分时指定的格式将应用于“正数;负数;零”。

可以对 bool 值调用 GetHashCode 方法以返回数字,以便您可以使用自定义数字格式字符串返回是/否或开/关或情况需要的任何其他单词集。

这是返回 on/OFF 的示例:

var truth   = string.Format("{0:on;0;OFF}", true.GetHashCode());
var unTruth = string.Format("{0:on;0;OFF}", false.GetHashCode());

返回:

truth   = on
unTruth = OFF

As the other answers indicate, the framework does not allow boolean values to have custom formatters. However, it does allow for numbers to have custom formats. The GetHashCode method on the boolean will return 1 for true and 0 for false.

According to MSDN Custom Numeric Format Strings, when there are 3 sections of ";" the specified format will be applied to "positive numbers;negative numbers;zero".

The GetHashCode method can be called on the bool value to return a number so you can use the Custom Numeric Format String to return Yes/No or On/Off or any other set of words the situation calls for.

Here is a sample that returns on/OFF:

var truth   = string.Format("{0:on;0;OFF}", true.GetHashCode());
var unTruth = string.Format("{0:on;0;OFF}", false.GetHashCode());

returns:

truth   = on
unTruth = OFF
白况 2024-11-07 21:28:36

不幸的是, Boolean.ToString(IFormatProvider) 在这里没有帮助:

provider 参数已保留。它不参与该方法的执行。这意味着,与大多数带有provider 参数的方法不同,Boolean.ToString(IFormatProvider) 方法不反映特定于区域性的设置。

无论如何,布尔值代表True和False,而不是Yes和No。如果你想映射True ->是与错 ->不,您必须自己完成此操作(包括本地化);框架中没有对此的内置支持。您提出的解决方案(Resources.Yes/No)对我来说看起来不错。

Unfortunately, Boolean.ToString(IFormatProvider) does not help here:

The provider parameter is reserved. It does not participate in the execution of this method. This means that the Boolean.ToString(IFormatProvider) method, unlike most methods with a provider parameter, does not reflect culture-specific settings.

In any case, Booleans represent True and False, not Yes and No. If you want to map True -> Yes and False -> No, you will have to do that (including localization) yourself; there's no built-in support in the framework for that. Your propopsed solution (Resources.Yes/No) looks fine to me.

云朵有点甜 2024-11-07 21:28:36

在我的剃刀页面中正在工作:
@(item.Active.GetValueOrDefault() ? "Yes" : "No")

这里很重要,如果属性是必需的(可以为空),如果需要,它应该很好用:

@(item.Active ? "Yes":"No")

我发现这个在其他帖子上,只是重播,希望对某些人有所帮助1。

in my razor page is working this:
@(item.Active.GetValueOrDefault() ? "Yes" : "No")

here is important if attribute is required (nullable) or not, if is required it should be good with:

@(item.Active ? "Yes":"No")

I found this here on some other post, just replaying, hope it helps some1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文