Click here to Skip to main content
15,897,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am trying to round up the decimal places of the values gotten from a dataset.

I have tried to use Math.round but it could not work.

Currently the decimal places is 17 and I want to round it up to 7 places.

Is that some command I could use to round up the values stored in a array from the dataset?

My subsequence calculation requires the round up values to do so I have to do the rounding up.

Thank you! :)
Posted

Round normally works fine; it does for me:
VB
Dim d As Double = 1.23456789012346
Dim r As Double = Math.Round(d, 8)
Console.WriteLine("{0} - {1} = {2}", d, r, d - r)
Gave:
1.23456789012346 - 1.23456789 = 1.23456800338317E-10
Note I used 8 digits to make it more obvious - 7 rounds the 9 up and it isn't as clear in the results. Still works though.
 
Share this answer
 
Comments
Boon How 19-Jan-12 20:21pm    
Hi Griff, thanks for the solution. I have found out the reason why Math.Round does not work for dataset. VB tells me that the dataset cannot be converted to decimal. Do you know what does that mean?
OriginalGriff 20-Jan-12 3:05am    
Yes - but I need to see the code it is complaining about! :laugh:
Just the line it objects to, and five or so each side for context maybe the whole function if it is short.
First, Math.Round always works, especially if you spell it correctly :-). Why would you tell us something which is not true?

More importantly, if you really work with floating-point numbers, rounding them before putting them in a database is a crime in nearly all situations. You don't need it. Database-stored floating-point values are never any "final" figures, then can later be used in some calculations as the intermediate or input values; and rounding errors in intermediate values tends to accumulate.

Even though you might need rounded values to be presented on screen, this is a very bad excuse for storing rounded values in the database. You never need it. If you need rounded presentation, this is not actually rounded, but formatting using, say, string.Format. Please see:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

Format specifiers shown in the last two links will help you to provide required presentation of data. Don't put it the string form in your database!

—SA
 
Share this answer
 
Comments
Boon How 19-Jan-12 20:43pm    
Hi SA, thanks for the link :) It has helped me to learn how to present. I think I have misused the word dataset. The situation is like this, I have set up a code to get data from a SQL server. Then I would like to use round up data from the SQL server to calculate. Is there a way for me to round up the values stored inside a array? Thanks :)

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