C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Rounding to nearest 10,100,1000, etc.


Posted Date: 05 Sep 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: ABitSmartMember Level: Diamond    
Rating: 1 out of 5Points: 10 (Rs 5)



The following code demonstrates how to round a number to the nearest 10 or 100 or 1000 or etc..


public int CustomCieling(int number)
{
int start = 10;
while( number/start != 0)
{
start = start*10;
}

return start;
}


The functionality is similar to the Math.Cieling function provided by the .Net framework. The difference is that, the Math.Cieling function rounds to the nearest integer greater than the number and my function rounds to the nearest 10's.

Test case input: 9
Test case output: 10

Test case input: 999
Test case output: 1000

Have fun.

For more details, visit http://abitsmart.com/?p=178



Responses to the resource: "Rounding to nearest 10,100,1000, etc."
Author: Mohan    06 Sep 2009Member Level: Diamond   Points : 1
Hi ABit,

Your Code is working for 9,99,999 perfectly.

But if i am giving 149,199,949 that time also it is return 1000.

Actually it should return like 150,200,950




Author: ABitSmart    07 Sep 2009Member Level: Diamond   Points : 1
Thank you very much for referring my post, appreciate your comments.

I did the test with your number and got 1000000000 as an answer. Which is correct. My code does rounding to the nearest 10,100,1000,10000 etc


Author: Mohan    07 Sep 2009Member Level: Diamond   Points : 0
Thanks for reply.

But you explain the code rounds near the 10's



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
.Net Rounding to nearest 10  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Search Page using Enums
Previous Resource: WPF ARGB
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources




About Us    Contact Us    Privacy Policy    Terms Of Use