הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, June 21, 2012 5:52 PM
I have created a new data column and in datacolumn.expression i want to round off the result. Is there any functions to round off..
All replies (9)
Thursday, June 21, 2012 6:17 PM ✅Answered | 2 votes
For all supported expressions, see http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
The supported function CONVERT implicitly rounds a value when converting a float type to an integer type. If you want to round to a specific number of decimal places, first multiply by 10 ^ x, then convert, then divide:
' Nearest whole number 'col.Expression = "CONVERT( DoubleCol, System.Int64 )"' One decimal place 'col.Expression = "CONVERT( DoubleCol * 10, System.Int64 ) / 10"' Two decimal places 'col.Expression = "CONVERT( DoubleCol * 100, System.Int64 ) / 100"' etc... '
jmh
Friday, June 22, 2012 2:48 AM
Blossom28
You can use Math.Round before string and string in brace.
Sunil Bhagwat
Friday, June 22, 2012 8:31 AM
Blossom,
You are facing the shortcomings of the datacolumn expression and one of the main reasons Linq was created.
The framework 1.0 and 1.1 where both relying on bankers rounding. Not much used by many people but very fair as banks give each other not a half penny. But that is not used. In version 2.0 came new rounding options but I've not seen them in the expression.
If you need however those methods in a bound datacontrol (grids) then you can use this sample on our website.
http://www.vb-tips.com/DataBindingEvents.aspx
The different options of Math rounding you can find here (look at midpoint rounding which you probably need)
http://msdn.microsoft.com/en-us/library/as4h66hd
Success
Cor
Sunday, June 24, 2012 1:44 PM
Thanks for your replies.
@Jmh_gr,
I added calculated column..I need to round the result of the calculated column. Below is my scenario:
I tried using Round. But It doesn't work for me.
System.Type decimalType;
decimalType = System.Type.GetType("System.Decimal");
DataColumn ctr = new DataColumn("abc%", decimalType);
ctr.AutoIncrement = false;
ctr.ReadOnly = true;
ctr.Expression = ("Convert(column1,'System.Decimal')/Convert(column2,'System.Decimal')*100");
//Result=0.07364237453256452376
//I have to round this result.
dsResults.Tables[0].Columns.Add(abc%);
Sunday, June 24, 2012 3:00 PM | 1 vote
You see that this is a VB.Net group?
To get a rounded result, you should use the expression as shown by Jmh, i.e. multiply the value by 100, convert it to Int64, divide it by 100. Your code does not do this.
Armin
Monday, June 25, 2012 1:57 PM
Your syntax (besides being C#, not VB.NET) is off. Also, I wrote that the point of using Convert is to convert to an integer type, and thus round the result, but you are converting to Decimal.
As for your syntax, you must call Columns.Add on the actual column object (ctr in your code), not the name of the column. Incidentally "abc%" is not even a valid identifier in C# or VB.NET so your code wouldn't even compile. Although you can include a percent sign in the name of your column as you have done I would strongly advise against it. Use something like "abc_percent" instead. You can always use a percent sign in the column header text if you are displaying your data in a DataGridView or something similar.
jmh
Tuesday, June 26, 2012 11:15 AM
@blossom28,
There is NO WAY you can achieve this at the expression column level as Microsoft hasn't given any function to do this.
All you can do is, round the value at the usage place.
If you are binding this table to Windows/Web grid, they allow you set expression at the grid column level, which can be done easily.
If you intend to display in console or in any other format , you just need to use Decimal.ToString("0.00");
Formatting methods:
http://msdn.microsoft.com/en-us/library/s8s7t687.aspx
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
http://www.csharp-examples.net/string-format-double/
Regards, Karthik D V Please vote if you find this posting was helpful or Mark it as answered.
Tuesday, June 26, 2012 12:43 PM | 1 vote
It is possible, I've given the sample links to that already Friday, the problem is, it is not one sentence to use it an the scenario's are then in a way that you cannot get it from internet with simply copy and past because every scenario is different.
Success
Cor
Monday, July 9, 2012 1:29 PM
Karthik,
you are correct. There is no way to achieve this at the expression column level.