Need a custom currency format to use with String.Format

前端 未结 5 669
無奈伤痛
無奈伤痛 2021-01-04 05:55

I\'m trying to use String.Format(\"{0:c}\", somevalue) in C# but am having a hard time figuring out how to configure the output to meet my needs. Here are my needs:

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 06:29

    Depending on if you are consistently using the same data type for all of your currency values, you could write an extension method that would make it so that your case is always met. For example if you were using the decimal type:

    public static string ToCurrencyString (this decimal value)
    {
      if (value == 0)
        return String.Empty;
      return value.ToString ("C");
    }
    

提交回复
热议问题