字符串格式错误的格式为%

时间:2013-12-05 13:47:36

标签: c# string-formatting

我想在图表中格式化轴。为此,我有以下几行:

chart.ChartAreas[series.Name].AxisY.LabelStyle.Format =
  "{0.# " + unit + ";-0.# " + unit + ";0 " + unit + "}";

unit =“Joule”的示例:Format = "{0.# Joule;-0.# Joule;0 Joule"}

它给我带来了不错的结果(例如1.5 - >“1.5 Joule”, - 1.4 - >“-1.4 Joule”)。

但如果unit = "%"值乘以100,则表示5 - > “500%”,1.3 - > “130%”......那是错的。还有一些输入,如“%”(字符串中有可变空格),“_%”,“‰”将数字加倍。

有没有办法显示百分比数量并防止这种影响?

请注意,我必须使用此格式Format = "???";中的格式,我不想操纵任何DataPoints(如每个DataPoint / 100)。

2 个答案:

答案 0 :(得分:2)

您可以将文字字符放在引号中,以避免将它们解释为格式代码:

chart.ChartAreas[series.Name].AxisY.LabelStyle.Format =
     "{0.# '" + unit + "';-0.# '" + unit + "';0 '" + unit + "'}";

答案 1 :(得分:1)

逃离百分号。

unit = @"\%";

unit = "\\%;