Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi,

I would like to remove fraction from my data table.

for example:

I have a following table:

A 98.581253
B 7.105125
C 101.325836

and I would like to show in datagridview in the following format:

A 98.58
B 7.11
C 101.32
Posted

If you want a formatting of the values the you can use the method .toString("0.00")
 
Share this answer
 
Comments
Member 10704857 2-Jun-15 1:50am    
how do it works?

please explain
Maciej Los 2-Jun-15 2:09am    
Sorry, but this answer is wrong (my vote of 1). ToString() method does not round decimal values to the specified number of decimal places. It's a string representation of the decimal number with pre-defined number of decimal places.
Sergey Alexandrovich Kryukov 2-Jun-15 2:14am    
You would be right, but only assuming that you would want to answer inquirer's question.
But answering this question is not the best help. Chances are, rounding is not needed, but formatting is exactly what is needed (even though rounding is hidden under the formatting).

How many cases you know when rounding is really needed? They are very rare. Moreover, rounding up is dangerous, because some calculations cause accumulation of rounding errors.

I'm going to vote 5 for this answer, which is the only one using some common sense, instead of just answering.

—SA
Maciej Los 2-Jun-15 2:19am    
Ok, i looked at the question one more time. You're right, Sergey. OP wants to display data in DGV. Upvoted!
Ralf Meier 2-Jun-15 2:59am    
Thank you Sergey
... and thank you too Maciej
After reading the data from database, you should use Decimal.Round()- if you are using decimal values or Math.Round() for double values.
 
Share this answer
 
v2
Comments
Member 10704857 2-Jun-15 1:36am    
I have tried this:

for (int x = 0; x < dtform.Rows.Count; x++)
{

decimal sub = decimal.Parse(dtform.Rows[x][3].ToString());


DGV1.Rows[x].Cells["A"].Value = Decimal.Round(sub);
}
the output is
A 99
B 7
C 101

I don't want this I would like set output with two decimal only
Raul Iloc 2-Jun-15 1:48am    
You should use also the 2nd paramter of the Round method, with the value 2 (the number of decimals that you want to be kept)!
Maciej Los 2-Jun-15 2:05am    
yes, +5!
https://msdn.microsoft.com/en-us/library/6be1edhb%28v=vs.110%29.aspx
Member 10704857 2-Jun-15 2:07am    
Thanks for your guidance Raul.
Raul Iloc 2-Jun-15 2:18am    
Welcome Maciej, I am glad that I could help you!
C#
for (int x = 0; x < dtform.Rows.Count; x++)
            {
                decimal sub = decimal.Parse(dtform.Rows[x][3].ToString());


                DGV1.Rows[x].Cells["A"].Value = decimal.Round(sub,2);
            }
 
Share this answer
 
v2
Comments
CHill60 2-Jun-15 7:26am    
After all of the effort the others went to just to help you, it is a bit rude to then answer your own question and even ruder to then formally accept it as the correct answer. Please don't do that in future.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900