Round number to next highest greatest place
Im trying to create a function that will return the next highest (for lack of a better term) "round" number, based on the greatest place digit (left most digit).
For example:
17 > 20
328 > 400
18564 > 20000
//Already round numbers will stay the same:
500 > 500
I know I can just do something like this:
int customRound(int i)
{
string s = i.ToString();
if (int.Parse(s.Substring(1)) > 0)
{
string greatestDigit = s.Substring(0, 1);
string digit = (int.Parse(greatestDigit) + 1).ToString();
return int.Parse(digit + string.Empty.PadRight(s.Length - 1, '0'));
}
return i;
}
But that just feels really hacky and Im sure theres a more elegant and mathematical way to do it.
c# math rounding
add a comment |
Im trying to create a function that will return the next highest (for lack of a better term) "round" number, based on the greatest place digit (left most digit).
For example:
17 > 20
328 > 400
18564 > 20000
//Already round numbers will stay the same:
500 > 500
I know I can just do something like this:
int customRound(int i)
{
string s = i.ToString();
if (int.Parse(s.Substring(1)) > 0)
{
string greatestDigit = s.Substring(0, 1);
string digit = (int.Parse(greatestDigit) + 1).ToString();
return int.Parse(digit + string.Empty.PadRight(s.Length - 1, '0'));
}
return i;
}
But that just feels really hacky and Im sure theres a more elegant and mathematical way to do it.
c# math rounding
Does 1 "round" to 10, 91 "round" to 100, etc
– PaulF
Nov 21 '18 at 17:27
add a comment |
Im trying to create a function that will return the next highest (for lack of a better term) "round" number, based on the greatest place digit (left most digit).
For example:
17 > 20
328 > 400
18564 > 20000
//Already round numbers will stay the same:
500 > 500
I know I can just do something like this:
int customRound(int i)
{
string s = i.ToString();
if (int.Parse(s.Substring(1)) > 0)
{
string greatestDigit = s.Substring(0, 1);
string digit = (int.Parse(greatestDigit) + 1).ToString();
return int.Parse(digit + string.Empty.PadRight(s.Length - 1, '0'));
}
return i;
}
But that just feels really hacky and Im sure theres a more elegant and mathematical way to do it.
c# math rounding
Im trying to create a function that will return the next highest (for lack of a better term) "round" number, based on the greatest place digit (left most digit).
For example:
17 > 20
328 > 400
18564 > 20000
//Already round numbers will stay the same:
500 > 500
I know I can just do something like this:
int customRound(int i)
{
string s = i.ToString();
if (int.Parse(s.Substring(1)) > 0)
{
string greatestDigit = s.Substring(0, 1);
string digit = (int.Parse(greatestDigit) + 1).ToString();
return int.Parse(digit + string.Empty.PadRight(s.Length - 1, '0'));
}
return i;
}
But that just feels really hacky and Im sure theres a more elegant and mathematical way to do it.
c# math rounding
c# math rounding
asked Nov 21 '18 at 17:16
Brian TackerBrian Tacker
46211431
46211431
Does 1 "round" to 10, 91 "round" to 100, etc
– PaulF
Nov 21 '18 at 17:27
add a comment |
Does 1 "round" to 10, 91 "round" to 100, etc
– PaulF
Nov 21 '18 at 17:27
Does 1 "round" to 10, 91 "round" to 100, etc
– PaulF
Nov 21 '18 at 17:27
Does 1 "round" to 10, 91 "round" to 100, etc
– PaulF
Nov 21 '18 at 17:27
add a comment |
2 Answers
2
active
oldest
votes
You can use Math.Log10 to determine the order-of-magnitude of the number (previous number that is a power of 10), then round up to the next multiple of it:
int customRound(int i)
{
var digits = (int)Math.Floor(Math.Log10(i));
var unit = (int)Math.Pow(10, digits);
return (int)(Math.Ceiling((double)i / unit) * unit);
}
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
|
show 1 more comment
int customRound(int i)
{
return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417408%2fround-number-to-next-highest-greatest-place%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Math.Log10 to determine the order-of-magnitude of the number (previous number that is a power of 10), then round up to the next multiple of it:
int customRound(int i)
{
var digits = (int)Math.Floor(Math.Log10(i));
var unit = (int)Math.Pow(10, digits);
return (int)(Math.Ceiling((double)i / unit) * unit);
}
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
|
show 1 more comment
You can use Math.Log10 to determine the order-of-magnitude of the number (previous number that is a power of 10), then round up to the next multiple of it:
int customRound(int i)
{
var digits = (int)Math.Floor(Math.Log10(i));
var unit = (int)Math.Pow(10, digits);
return (int)(Math.Ceiling((double)i / unit) * unit);
}
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
|
show 1 more comment
You can use Math.Log10 to determine the order-of-magnitude of the number (previous number that is a power of 10), then round up to the next multiple of it:
int customRound(int i)
{
var digits = (int)Math.Floor(Math.Log10(i));
var unit = (int)Math.Pow(10, digits);
return (int)(Math.Ceiling((double)i / unit) * unit);
}
You can use Math.Log10 to determine the order-of-magnitude of the number (previous number that is a power of 10), then round up to the next multiple of it:
int customRound(int i)
{
var digits = (int)Math.Floor(Math.Log10(i));
var unit = (int)Math.Pow(10, digits);
return (int)(Math.Ceiling((double)i / unit) * unit);
}
answered Nov 21 '18 at 17:27
Klaus GütterKlaus Gütter
2,45311321
2,45311321
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
|
show 1 more comment
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
Perfect solution, exactly what I was looking for, thank you.
– Brian Tacker
Nov 21 '18 at 17:32
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
This solution does leave values 1-9 unchanged - is that correct?
– PaulF
Nov 21 '18 at 17:40
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
@PaulF as it does 10, 20, ... That's how I understood the requirement.
– Klaus Gütter
Nov 21 '18 at 17:42
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
All other numbers round up to the next value ending with 0. If the requirement is to retain the same number of digits, then 91 should not round to 100.
– PaulF
Nov 21 '18 at 17:47
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
@PaulF but what else should 91 round to given the requirement "next highest"?
– Klaus Gütter
Nov 21 '18 at 17:49
|
show 1 more comment
int customRound(int i)
{
return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
}
add a comment |
int customRound(int i)
{
return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
}
add a comment |
int customRound(int i)
{
return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
}
int customRound(int i)
{
return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
}
answered Nov 21 '18 at 17:32
Ehsan.SaradarEhsan.Saradar
45337
45337
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417408%2fround-number-to-next-highest-greatest-place%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does 1 "round" to 10, 91 "round" to 100, etc
– PaulF
Nov 21 '18 at 17:27