Round number to next highest greatest place












1















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.










share|improve this question























  • Does 1 "round" to 10, 91 "round" to 100, etc

    – PaulF
    Nov 21 '18 at 17:27
















1















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.










share|improve this question























  • Does 1 "round" to 10, 91 "round" to 100, etc

    – PaulF
    Nov 21 '18 at 17:27














1












1








1


1






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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















6














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);
}





share|improve this answer
























  • 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



















1














int customRound(int i)
{
return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
}





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    6














    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);
    }





    share|improve this answer
























    • 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
















    6














    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);
    }





    share|improve this answer
























    • 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














    6












    6








    6







    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);
    }





    share|improve this answer













    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);
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    1














    int customRound(int i)
    {
    return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
    }





    share|improve this answer




























      1














      int customRound(int i)
      {
      return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
      }





      share|improve this answer


























        1












        1








        1







        int customRound(int i)
        {
        return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
        }





        share|improve this answer













        int customRound(int i)
        {
        return (int)(Math.Ceiling(i / Math.Pow(10, i.ToString().Length-1)) * Math.Pow(10, i.ToString().Length - 1));
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 17:32









        Ehsan.SaradarEhsan.Saradar

        45337




        45337






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?