如何添加百分比符号(%) [英] How to add percentage symbol(%)
问题描述
Hi Frnds,
我想在C#代码的数据表中以百分比列值的形式添加%符号作为后缀。
我尝试过:
我已经尝试过:
foreach(DataRow卓尔在dt.Rows)
{
drow [PER] = string.Concat(drow.Field< decimal>(PER),%) ;
}
这里我的PER列是十进制值。我不能连贯这些价值观。任何人都可以帮忙解决问题。
先谢谢。
不要试图更改您的DataTable - 这可能是一个坏主意,因为它可能链接到数据源。向源添加%意味着将其从数值更改为字符串,这将是一个非常糟糕的主意!
相反,请查看格式当您向用户呈现数据时 - DataGridView等都具有格式化选项,允许您显示所需的百分号。我无法告诉你该做什么 - 我不知道你使用的是DataTable,甚至是你呈现数据的环境 - 但是如果谷歌控制名称和格式化,MSDN文档应该能够提供帮助。
您不能简单地在号码后添加[%]。PER
列是数字类型(可能是十进制),因此无法存储文本!
要解决此问题,您可以添加 computed / expression栏 [ ^ ]。请参阅:
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( PER, typeof ( decimal )));
dt.Rows.Add( new object [] { 0 。 85 });
dt.Rows.Add( new object [] { 0 。 99 });
dt.Rows.Add( new object [] { 0 。 67 });
dt.Rows.Add( new object [] { 0 。 46 });
dt.Rows.Add( new object [] { 0 。 27 });
DataColumn dc = new DataColumn( PER2, typeof ( string ));
dc.Expression = (PER * 100)+'%';
dt.Columns.Add(dc);
结果:
PER PER2
0,85 85,00%
0,99 99,00%
0,67 67,00%
0,46 46,00%
0,27 27,00%
详情请见:DataColumn.Expression属性(System.Data) [ ^ ]
Hi Frnds,
I wan to add % symbol as suffix in percentage column value in datatable in C# code.
What I have tried:
I already tried with:
foreach (DataRow drow in dt.Rows)
{
drow["PER"] = string.Concat(drow.Field<decimal>("PER"), "%");
}
Here my PER column was in decimal value . I can't concat the values. Anyone please help to resolve the issue.
Thanks in Advance.
Don't try to change your DataTable - that's a bad idea as it may be linked to the data source. And adding a "%" to the source would mean changing it from a numeric value to a string, which would be a very bad idea!
Instead, look at formatting the data when you present it to the user - DataGridView and suchlike all have formatting options which would allow you to display the percentage sign you require. I can't tell you what to do - I have no idea what you are using the DataTable with, or even the environment you are presenting data in - but the MSDN documentation should be able to help if you Google the control name and "Formatting".
You can't simply add [%] right after the number. APER
column is type of numeric (probably decimal), so it can't store a text!
To resolve it, you can add computed/expression column[^]. See:
DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("PER", typeof(decimal))); dt.Rows.Add(new object[]{0.85}); dt.Rows.Add(new object[]{0.99}); dt.Rows.Add(new object[]{0.67}); dt.Rows.Add(new object[]{0.46}); dt.Rows.Add(new object[]{0.27}); DataColumn dc = new DataColumn("PER2", typeof(string)); dc.Expression = "(PER*100) + '%'"; dt.Columns.Add(dc);
Result:
PER PER2 0,85 85,00% 0,99 99,00% 0,67 67,00% 0,46 46,00% 0,27 27,00%
For further details, please see: DataColumn.Expression Property (System.Data)[^]
这篇关于如何添加百分比符号(%)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!