.net ToString() format documentation

后端 未结 5 1437
感情败类
感情败类 2021-02-20 08:06

I saw a code snippet the other day that converts a Boolean value to the corresponding \"Yes\"/\"No\" value:

CDbl(True).ToString(\"Yes;Yes;No\")

5条回答
  •  悲哀的现实
    2021-02-20 08:41

    As @Joel Coehoorn and @tvanfosson said, it's using a custom numeric format string. The reason it works is that a boolean value is convertible to a double using the following (essentially):

    public static double ToDouble(bool value)
    {
        return (value ? ((double) 1) : ((double) 0));
    }
    

    So, if value is true, it returns 1 and if value is false it returns 0. At that point, the section mapping rules apply as described by @tvanfosson (and subsequently @Joel Coehoorn).

提交回复
热议问题