Jump to content

Round up whole numbers


prichardson

Recommended Posts

How do you round how numbers in asp?For example if it 47 but i want it to be 45. Or if it was 48 then i want it to be 50.I understand the deminal round up to whole numbers - but can you do it for whole numbers?This is code that works for rounding up deminals - do i need to adapt this code :

strOverall_Usage_Graph = Round(strOverall_Usage_Graph)

Or is the code for whole numbers completely different?Many Thanks

Link to comment
Share on other sites

Rounding a number rounds to the nearest whole number, rounding doesn't round to the nearest multiple of 5 or whatever you want to do. If you want to round by 10s you could divide the number by 10, then round, then multiply by 10, but to round to the nearest 5 you will need to use the mod function to calculate the remainder and then decide (based on the remainder) if the number should be 0, 5, or 10. Look at the mod function in VBscript to calculate the remainder, if you mod 47 by 10 the result will be 7, and you can look at that and say that 7 should be either rounded down to 5 or up to 10, and add that to 40 to get either 45 or 50.

Link to comment
Share on other sites

You could jsut divide the number by five and round, then multiply by five...

strOverall_Usage_Graph = strOverall_Usage_Graph / 5strOverall_Usage_Graph = Round(strOverall_Usage_Graph)strOverall_Usage_Graph = strOverall_Usage_Graph * 5

So, take 47, divide by 5 = 9.4, round = 9, times 5 = 45. Similarly, 48 divide by 5 = 9.6, round = 10, times 5 = 50.

Link to comment
Share on other sites

Cheers Synook,That was just what i was looking for - i tested it by making strOverall_Usage_Graph = 47 and with your code it worked - but i did that just to test quickly, so i took out that bit so it should pull the number from the db table and should do the calculation you gave me to work too.But it gives me an error - Type mismatchon this line - strOverall_Usage_Graph = strOverall_Usage_Graph / 5I have the db field type as numeric.I also tried making the number 5 as a string - as below but that also did not work. Any ideas to why i get this error and what might be the problem?

strOverall_Usage_Graph = rsSTB.Fields.Item("Overall_Usage").Valuedim strdividebyfivedim strtimesbyfivestrdividebyfive = 5strtimesbyfive = 5strOverall_Usage_Graph = (strOverall_Usage_Graph) / (strdividebyfive)strOverall_Usage_Graph = Round(strOverall_Usage_Graph)strOverall_Usage_Graph = (strOverall_Usage_Graph) * (strtimesbyfive)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...