Click here to Skip to main content
12,078,489 members (45,333 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked

Round up a number to greatest 10s place

, 24 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Recently we faced a situation which we need to round up a number to maximum 10s place

Recently we faced a situation which we need to round up a number to greatest 10s place.

For ex:  we needed to round up a numbers as follows
1023 -> 2000
37911 -> 40000
912345 –> 1000000

First we checked Math.Round(). But it didn’t solve our problem.  Math.Round() supports only rounds a value to the nearest integer or to the specified number of fractional digits. Further checking we come up a solution by using Math.Ceiling() method.

Math.Ceiling()returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
For ex:
Math.Ceiling(7.3)     -> 8
Math.Ceiling(7.64)     -> 8
Math.Ceiling(0.12)     -> 1

Below is the solution we came up

decimal[] values = { 1023, 37911, 23000, 1234, 912345 };
foreach (var value in values) {
//// get the lenght of the 10s places;
var length = value.ToString().Length - 1;
//// using Math.Pow() calculate the power of 10s
var power = Math.Pow(10, (double)length);
//// var roundadValue = Math.Ceiling(1023 / 1000) * 1000;
var roundadValue = Math.Ceiling(value / (decimal)power) * (decimal)power;

Console.WriteLine("Value:{0} Rounded Value:{1}", value, roundadValue); 


Math.Ceiling() Results

The Logic

Math.Ceiling(value / (decimal)power) * (decimal)power;
var roundadValue = Math.Ceiling(1023 / 1000) * 1000;

so, for the any number, fist calculate power of 10s and then it use for above calculation to get the results.


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


About the Author

Tharaka MTR
Technical Lead Eyepax IT Consulting (Pvt) Ltd.
Sri Lanka Sri Lanka
Having more than 9 year hands-on industry experience in software development
Responsible for designing, implementing and managing complex software systems with stringent up-time requirement.

Visit my blog

You may also be interested in...

Comments and Discussions

QuestionSituation NOT Citation Pin
deebee++23-Jul-14 4:27
professionaldeebee++23-Jul-14 4:27 
AnswerRe: Situation NOT Citation Pin
Tharaka MTR23-Jul-14 22:53
professionalTharaka MTR23-Jul-14 22:53 
SuggestionNice solution, but Pin
sashadu22-Jul-14 4:18
membersashadu22-Jul-14 4:18 
GeneralRe: Nice solution, but Pin
Tharaka MTR22-Jul-14 7:02
professionalTharaka MTR22-Jul-14 7:02 
GeneralRe: Nice solution, but Pin
George Swan22-Jul-14 11:41
memberGeorge Swan22-Jul-14 11:41 
GeneralRe: Nice solution, but Pin
Tomas Ruksenas18-Aug-14 4:57
memberTomas Ruksenas18-Aug-14 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160212.1 | Last Updated 24 Jul 2014
Article Copyright 2014 by Tharaka MTR
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid